Leetcode: Elimination Game
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list. Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers. We keep repeating the steps again, alternating left to right and right to left, until a single number remains. Find the last number that remains starting with a list of length n. Example: Input:
n = 9,
1 2 3 4 5 6 7 8 9
2 4 6 8
2 6
6 Output:
6
refer to https://discuss.leetcode.com/topic/59293/java-easiest-solution-o-logn-with-explanation
Time Complexity: O(log n)
update and record head in each turn. when the total number becomes 1, head is the only number left.
When will head be updated?
- if we move from left
- if we move from right and the total remaining number % 2 == 1
like 2 4 6 8 10, we move from 10, we will take out 10, 6 and 2, head is deleted and move to 4
like 2 4 6 8 10 12, we move from 12, we will take out 12, 8, 4, head is still remaining 2
then we find a rule to update our head.
public class Solution {
public int lastRemaining(int n) {
int remaining = n;
int head = 1;
boolean fromLeft = true;
int step = 1;
while (remaining > 1) {
if (fromLeft || remaining%2 != 0) {
head += step;
}
remaining /= 2;
fromLeft = !fromLeft;
step *= 2;
}
return head;
}
}
Leetcode: Elimination Game的更多相关文章
- [LeetCode] Elimination Game 淘汰游戏
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...
- 【LeetCode】390. Elimination Game 解题报告(Python)
[LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...
- [LeetCode] 390. Elimination Game 淘汰游戏
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...
- [leetcode] 390 Elimination Game
很开心,自己想出来的一道题 There is a list of sorted integers from 1 to n. Starting from left to right, remove th ...
- LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...
- 【leetcode】390. Elimination Game
题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] - ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Rotate Function 旋转函数
Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotati ...
- [LeetCode] Candy Crush 糖果消消乐
This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...
随机推荐
- BAT批处理(二)
在前一篇中已对BAT批处理基础作了一些总结,但是对于BAT批处理还有很多的知识点没有讲解到,比如DOS中的特殊符号:IF.FOR的使用:变量:更多的DOS命令等等.本文在前一篇的基础上继续对BAT批处 ...
- JMeter使用点滴
作为一款小巧易用的性能测试工具,JMeter不仅免费开源而且功能强大.易于扩展,如果有一定Java开发基础的话还可以在JMeter上做扩展开发新的插件等,几乎能满足各种性能测试需求,本文用于收集使用J ...
- mysql 锁
Lock table有两种模式 lock tables table_name read [or write]; test1: session 1: lock tables tmp_xf_lock; ...
- CentOS_6.5安装Nginx+PHP+MySQL
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH # Ch ...
- 打造 PHP版本 1password
以前注册很多网站密码都使用简单密码,但是由于今年频繁曝出密码不安全问题,所以要使用更加复杂的密码.但是好多个账号,密码也不能设置成一样的,防止一个被盗全部不安全了,记密码就成了意见很头疼的事情. 在手 ...
- Uploadify上传问题
版本:Uploadify Version 3.2官网:http://www.uploadify.com Uploadify是一款基于Jquery的上传插件,用起来很方便.但上传过程中的提示语言为英文, ...
- BlueDroid介绍
目录 1. 基本结构 2. 代码区 自从Android 4.2开始,Android开始使用自己的蓝牙协议栈BlueDroid,而不是bluez BlueDroid可分为两层: - BTE: Bluet ...
- ngrok的使用
windows的ngrok配置: 步骤一:下载ngrok http://pan.baidu.com/s/1jH0s8o2 步骤二:如果你的国外网址没被墙就直接使用cmd命令行使用. 国内ngrok配置 ...
- 为mutable类型的容器(array,set等)添加kvo,有点麻烦,供参考和了解下吧
http://blog.csdn.net/caryaliu/article/details/49284185 需要在被观察的属性所在的类里面实现一些方法,对开发者不友好,一般不建议使用,这里mark一 ...
- c#导出excel(转)
C#导出Excel文件实例代码 2010-08-03 14:10:36| 分类: 软件编程 | 标签:excel c#导出excel |字号大中小 订阅 /// <summary> ...