【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 ...
随机推荐
- Oracle-where exists()、not exists() 、in()、not in()用法以及效率差异
0.exists() 用法: select * from T1 where exists(select 1 from T2 where T1.a=T2.a) 其中 "select 1 fro ...
- Perl哈希%hash
哈希是 key/value 键/值对的集合. Perl中哈希变量以百分号 (%) 标记开始. 访问哈希元素格式:${key}. 以下是一个简单的哈希实例: 实例 #!/usr/bin/perl %da ...
- Go语言缺陷
我为什么放弃Go语言 目录(?)[+] 我为什么放弃Go语言 有好几次,当我想起来的时候,总是会问自己:我为什么要放弃Go语言?这个决定是正确的吗?是明智和理性的吗?其实我一直在认真思考这个问题. 开 ...
- Freeswitch 安装爬坑记录1
2 Freeswitch的安装 2.1 准备工作 服务器安装CentOS 因为是内部环境,可以关闭一些防火墙设置,保证不会因为网络限制而不能连接 关闭防火墙 查看防火墙 systemctl statu ...
- 日常Java 2021/9/28
字符串反转 package m; public class m { public static void main(String[] args) { //定义一个字符串 String str = &q ...
- absent, absolute
absent 1. A teacher asked in a class who killed Abraham Lincoln. A blonde said "It wasn't me, I ...
- LVM磁盘创建与扩容
以虚拟机为例 1.在虚拟机上添加新磁盘,点击虚拟机→设置->添加,最后如下图. 2.进入系统fdisk -l,查看当前磁盘信息 [root@master shell]# fdisk -l Dis ...
- OSGi系列 - 使用Eclipse查看Bundle源码
使用Eclipse开发OSGi Bundle时,会发现有很多现成的Bundle可以用.但如何使用这些Bundle呢?除了上网搜索查资料外,阅读这些Bundle的源码也是一个很好的方法. 本文以org. ...
- UIButton总结
UIButton 1. 功能 既能显示文字,又能显示图片(能显示2张图片,背景图片.内容图片) 长按高亮的时候可以切换图片\文字 直接通过addTarget...方法监听点击 2. 状态 normal ...
- Spring Boot项目的不同启动方式
方式一: 直接通过IntelliJ IDEA启动,直接执行Spring Boot项目的main()方法. 方法二: 将项目打包成jar包,首先需要在pom.xml文件的根节点下添加如下配置: < ...