【LeetCode】390. Elimination Game 解题报告(Python)
【LeetCode】390. Elimination Game 解题报告(Python)
标签: LeetCode
题目地址:https://leetcode.com/problems/elimination-game/description/
题目描述:
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
题目大意
消除游戏。
分为两种消除的方式,第一种是从左向右,从第一个位置开始间隔着消除;第二种是从右向左,从最后一个位置开始间隔这消除。
求最后能留下的最后的结果是哪个数。
解题方法
看到这个题,我一定能肯定,这个题的不是使用暴力去求解的,否则没有意义。这个题的做法非常巧妙。消除的规律是同样的,只不过顺序不同,那么,可以封装成两个函数,互相调用达到目的。这是建立在这个思想上的:
第一次从左向右检索完,剩下,2 4 6 8, 其实这跟1 2 3
4的信息几乎是一样的,只是差了倍数2,所以问题就变为从右往左对规模4的问题进行操作,找到答案乘以2就行。对于从右往左,如果是1 2 3 4
5的话,检索完还剩2 4,同样是1 2的问题,如果是 1 2 3 4,剩 1 3,我们可以认为是1
2乘以2减一,总之,我们可以找到将每次的剩余子序列转化为同类子问题的方法。
这个算是分而治之的思想,很巧妙的通过2倍关系实现了递归调用。
class Solution(object):
def lastRemaining(self, n):
"""
:type n: int
:rtype: int
"""
return self.leftToRight(n)
def leftToRight(self, n):
if n == 1: return 1
if n == 2: return 2
if n & 1 == 1:
return 2 * self.rightToLeft((n - 1) / 2)
else:
return 2 * self.rightToLeft(n / 2)
def rightToLeft(self, n):
if n == 1: return 1
if n == 2: return 1
if n & 1 == 1:
return 2 * self.leftToRight((n - 1) / 2)
else:
return 2 * self.leftToRight(n / 2) - 1
日期
2018 年 3 月 12 日
【LeetCode】390. Elimination Game 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- BAT的一些题
114.java中实现多态的机制是什么 答:重写,重载.方法的重写Overriding和重载Overloading是Java多态性的不同表现. 重写Overriding是父类与子类之间多态性的一种表 ...
- 日常Java 2021/11/3
java网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来.java.net包中J2SE的APl包含有类和接口,它们提供低层次的通信细节.你可以直接使用这些类和接口, ...
- A Child's History of England.42
The names of these knights were Reginald Fitzurse, William Tracy, Hugh de Morville, and Richard Brit ...
- 关于learning Spark中文版翻译
在网上找了很久中文版,感觉都是需要支付一定金币才能下载,索性自己翻译算了.因为对Spark有一定了解,而且书籍前面写道,对Spark了解可以直接从第三章阅读,就直接从第三章开始翻译了,应该没有什么 ...
- 一道题目学ES6 API,合并对象id相同的两个数组对象
var arr2=[{id:1,name:'23'}] var arr1=[{id:1,car:'car2'}] const combined = arr2.reduce((acc, cur) =&g ...
- 从源码看Thread&ThreadLocal&ThreadLocalMap的关系与原理
1.三者的之间的关系 ThreadLocalMap是Thread类的成员变量threadLocals,一个线程拥有一个ThreadLocalMap,一个ThreadLocalMap可以有多个Threa ...
- Bash shell(六)-管道命令
就如同前面所说的, bash 命令执行的时候有输出的数据会出现! 那么如果这群数据必需要经过几道手续之后才能得到我们所想要的格式,应该如何来设定? 这就牵涉到管线命令的问题了 (pipe) ,管线命令 ...
- clickhouse 输入输出格式
TabSeparated.TabSeparatedRaw.TabSeparatedWithNames和TabSeparatedWithNamesAndTypes TabSeparated 默认格式,缩 ...
- mysql之对象创建
1 --创建表空间 2 create tablespace tablespace_name 3 innodb and ndb: 4 add datafile 'file_name' 5 innodb ...
- zookeeper 异常 :stat is not executed because it is not in the whitelist. Connection closed b
1 .问题 1.启动 zookeeper 后 用指令: telnet 127.0.0.1 2181 连接 提示输入指令 :stat 后报错,然后关闭连接 2.问题解决: 修改启动指令 zkServe ...