【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 ...
 
随机推荐
- Linux—yum的python版本错误——高级解决方案
			
彻底搞明白,python升级后,为什么会导致yum不可用 首先我们来分析下,python升级后,yum为什么会不可用? 先说个关于python的问题,Linux系统很多软件都依赖于python,因此不 ...
 - Linux— rpm 命令
			
rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...
 - 10.Power of Two-Leetcode
			
Given an integer, write a function to determine if it is a power of two. class Solution { public: bo ...
 - Excel-给出指定数值的日期 date()
			
DATE函数 函数名称:DATE 主要功能:给出指定数值的日期. 使用格式:DATE(year,month,day) 参数说明:year为指定的年份数值(小于9999):month为指定的月份数值(可 ...
 - Android 高级UI组件(一)GridView与ListView
			
1.GridView 1.GridView学习 GridView和ListView都是比较常用的多控件布局,而GridView更是实现九宫图的首选 main.xml: <?xml version ...
 - android知识点duplicateParentState
			
android知识点duplicateParentState 今天要做一个效果,组件RelativeLayout上有两个TextView,这两个TextView具有不同的颜色值,现在要的效果是,当Re ...
 - Default arguments and virtual function
			
Predict the output of following C++ program. 1 #include <iostream> 2 using namespace std; 3 4 ...
 - 分布式系统为什么不用自增id,要用雪花算法生成id???
			
1.为什么数据库id自增和uuid不适合分布式id id自增:当数据量庞大时,在数据库分库分表后,数据库自增id不能满足唯一id来标识数据:因为每个表都按自己节奏自增,会造成id冲突,无法满足需求. ...
 - 【面试】【Linux】【Web】基础概念
			
1. HTTP https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 2. TCP handshake https://en.wikipe ...
 - awk统计命令(求和、求平均、求最大值、求最小值)
			
本节内容:awk统计命令 1.求和 cat data|awk '{sum+=$1} END {print "Sum = ", sum}' 2.求平均 cat data|awk '{ ...