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的更多相关文章

  1. [LeetCode] Elimination Game 淘汰游戏

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...

  2. 【LeetCode】390. Elimination Game 解题报告(Python)

    [LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...

  3. [LeetCode] 390. Elimination Game 淘汰游戏

    There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...

  4. [leetcode] 390 Elimination Game

    很开心,自己想出来的一道题 There is a list of sorted integers from 1 to n. Starting from left to right, remove th ...

  5. 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) ...

  6. 【leetcode】390. Elimination Game

    题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] - ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [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 ...

  9. [LeetCode] Candy Crush 糖果消消乐

    This question is about implementing a basic elimination algorithm for Candy Crush. Given a 2D intege ...

随机推荐

  1. LR脚本技巧

    1.参数化空值       如上图所示,当参数化时某个值需要为空值(非空格),直接在参数化文件中空一行/格即可,虽然Parameter List界面上没有显示空的那一行,但并不影响取值. 2.手工日志 ...

  2. 初步理解Java的三大特性——封装、继承和多态

    声明:整理自网络,如有雷同,请联系博主处理 一.封装 封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被 ...

  3. android ListView详解继承ListActivity

    [转]http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形式展 ...

  4. 删除win7中的库/收藏夹/家庭组/网络

    通过修改注册表删除库/收藏夹/家庭组/网络(还是不习惯库的这种管理方式, 导航里面又太占地方) 库:[HKEY_CLASSES_ROOT\CLSID\{031E4825-7B94-4dc3-B131- ...

  5. In App Purchase

    参考文章1,参考文章2,参考文章3, 参考文章3 一.Product 分类:      Consumables: 应该在应用里被消费掉的.(Coupon, 生命数)      Non-Consumab ...

  6. install zabbix-agent on CENTOS

    in ubuntu--https://www.digitalocean.com/community/tutorials/how-to-install-zabbix-on-ubuntu-configur ...

  7. yaf性能测试(wamp环境)

    1实现mvc 出现helloword,成功 2.controller重定向 $get = $this->getRequest()->getQuery("get", &q ...

  8. 【Java IO】FileInputStream 和 FileOutputStream

    class FileInputStream extends  InputStream implements Closeable

  9. Mysql 只导出数据,不包含表结构

    mysqldump -u${user} -p${passwd} --no-create-info --database ${dbname} --table ${tablename} > ${ta ...

  10. 框架,公共模块,unified思想

    最近两周一直在加班加点refactor代码,贡献了2014年最后一个周末和2015年元旦三天假期,终于赶在了sprint结束之前完成. 可见,这个sprint做的并不理想! 项目逻辑本身并不复杂,从数 ...