题目

https://leetcode.com/problems/elimination-game/

题解

方法1:模拟(内存超出限制)

最朴素的思路,模拟每一轮的消除,直到剩余大小为 1 为止。

此方法内存超限。

class Solution {public int lastRemaining(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) {a[i] = i + 1;}int size = n;int[] b = null;boolean L = true; // start from leftwhile (size >= 1) {size /= 2;b = new int[size]; // 每轮缩容一半int p = 0;if (L) {while (p < size) {b[p] = a[p * 2 + 1];p++;}L = false;} else {while (p < size) {b[size - p - 1] = a[a.length - (p * 2 + 1) - 1];p++;}L = true;}int[] t = a;a = b;b = t;}return b[0];}
}

方法2:只记录起始位置以及步长(通过)

参考:JAVA: Easiest solution O(logN) with explanation

My idea is to 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.

example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

  1. Let us start with head = 1, left = true, step = 1 (times 2 each turn), remaining = n(24)

  2. we first move from left, we definitely need to move head to next position. (head = head + step)
    So after first loop we will have:
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 - > 2 4 6 8 10 12 14 16 18 20 22 24
    head = 2, left = false, step = 1 * 2 = 2, remaining = remaining / 2 = 12

  3. second loop, we move from right, in what situation we need to move head?
    only if the remaining % 2 == 1, in this case we have 12 % 2 == 0, we don’t touch head.
    so after this second loop we will have:
    2 4 6 8 10 12 14 16 18 20 22 24 - > 2 6 10 14 18 22
    head = 2, left = true, step = 2 * 2 = 4, remaining = remaining / 2 = 6

  4. third loop, we move from left, move head to next position
    after third loop we will have:
    2 6 10 14 18 22 - > 6 14 22
    head = 6, left = false, step = 4 * 2 = 8, remaining = remaining / 2 = 3

  5. fourth loop, we move from right, NOTICE HERE:
    we have remaining(3) % 2 == 1, so we know we need to move head to next position
    after this loop, we will have
    6 14 22 - > 14
    head = 14, left = true, step = 8 * 2 = 16, remaining = remaining / 2 = 1

  6. while loop end, return head

class Solution {public int lastRemaining(int n) {boolean L = true; // 是否从左向右int size = n;int step = 1;int begin = 1; // 开始的数字(而非位置)while (size > 1) {if (L || size % 2 == 1) {begin += step;}size /= 2;step *= 2;L = !L;}return begin;}
}

leetcode 390. Elimination Game | 390. 消除游戏(Java)相关推荐

  1. 【leetcode】2022.1.2 消除游戏

    390. 消除游戏 分析 这题看完像是抓住了什么,但又像啥也没抓住. 当没有思路的时候,最好的解题思路就是先用最暴力最弱智的办法把它做出来,然后再进行优化. 暴力模拟法 - 真的弄出来个数组,按照他的 ...

  2. 卡片消除游戏 java版(代码+讲解)

    最开始一出的时候就准备写了,被别的事情耽误了.花了两天时间,把某个游戏的基本的功能都写出来了,还是挺简单的.简单来说就是个卡片消除游戏,代码方面挺简单,比代码难的是地图的制作(卡片的放置).完整代码在 ...

  3. [算法]LeetCode每日一题--174. 地下城游戏(Java)

    DailyChallenge 174. 地下城游戏 Hard20200712 Description 一些恶魔抓住了公主(P)并将她关在了地下城的右下角.地下城是由 M x N 个房间组成的二维网格. ...

  4. Java实现 LeetCode 390 消除游戏

    390. 消除游戏 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数 ...

  5. [LeetCode]390.消除游戏

    [LeetCode]390.消除游戏 题目 示例 方法 模拟 题目 列表 arr 由在范围 [1, n] 中的所有整数组成,并按严格递增排序.请你对 arr 应用下述算法: 从左到右,删除第一个数字, ...

  6. LeetCode:390. 消除游戏————中等

    题目 390. 消除游戏 列表 arr 由在范围 [1, n] 中的所有整数组成,并按严格递增排序.请你对 arr 应用下述算法: 从左到右,删除第一个数字,然后每隔一个数字删除一个,直到到达列表末尾 ...

  7. LeetCode 390. 消除游戏

    ​​​​​​390. 消除游戏 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始 ...

  8. LeetCode刷题——消除游戏#390#Medium

    消除游戏的思路探讨与源码     消除游戏的题目如下图,该题属于递归类和数学类型的题目,主要考察对于数学方法的使用和递归方法的理解.本文的题目作者想到2种方法,分别是递归方法和数学模拟方法,其中递归方 ...

  9. 390. 消除游戏(约瑟夫环)

    390. 消除游戏 列表 arr 由在范围 [1, n]中的所有整数组成,并按严格递增排序.请你对 arr 应用下述算法: 从左到右,删除第一个数字,然后每隔一个数字删除一个,直到到达列表末尾. 重复 ...

最新文章

  1. 「linux」win+linux 双系统 默认启动项 的修改
  2. [Lua]50行代码的解释器,用来演示lambda calculus
  3. nginxmysql负载均衡,神操作!
  4. 【转】利用WCF的双工通信
  5. OS中关于父子进程的执行顺序和多个子进程之间的执行顺序(整理)
  6. OpenVINO 2020版没有cpu_extension.dll问题解决
  7. 【registry】NoSuchFieldError: INCLUDE_ALL
  8. 为什么黑客都用python-为什么如此多的黑客都用python?
  9. atitit 用什么样的维度看问题.docx 如何了解 看待xxx
  10. Java内存解析 程序的执行过程
  11. Oracle中登录OEM口令忘记,oracle oem创建过程 一直提示sys密码错误
  12. [转帖]变速齿轮的一种实现方法(内有中断门的创建与调用)
  13. VTP(VLAN中继协议/虚拟局域网干道协议 VLAN Trunking Protocol)
  14. 如何升级到 Ubuntu 20.04
  15. php 均匀随机算法,PHP算法学习(4) 随机算法
  16. 【渝粤教育】电大中专跨境电子商务理论与实务 (17)作业 题库
  17. 区块链技术应用后,不再需要CA认证机构
  18. openwrt支持wpa3加密
  19. 文本识别OCR浅析:特征篇
  20. SCAP标准协议和威胁情报关键词术语

热门文章

  1. 证件照排版软件_证件照小程序换背景(制作免费版)
  2. 第一个Canvas实例-钟表
  3. assert()函数
  4. base64编解码的类
  5. 对现有的所能找到个DDOS代码(攻击模块)做出一次分析----TCP篇
  6. OpenCV_006-OpenCV 轨迹栏作为调色板
  7. 什么是Flink?Flink能用来做什么?
  8. Linux网络编程 | 零拷贝 :sendfile、mmap、splice、tee
  9. 操作系统 : 按优先数调度算法实现处理器调度(C++)
  10. 重新深入理解零拷贝技术