390. Elimination Game

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

这道题dp的方法如下,dp能比较好的显示思路
public int LastRemaining(int n) {
        if(n<=2) return n;
        if(n==3) return 2;
        var left = new int[n+1];
        var right = new int[n+1];
        left[1] = 1;
        right[1] = 1;
        left[2] = 2;
        right[2] = 1;
        for(int i= 3;i<=n;i++)
        {
            if(i%2 == 0)
            {
                 left[i] = 2*right[i/2];
                 right[i] = 2*left[i/2]-1;
            }
            else
            {
                 left[i] = 2*right[i/2];
                 right[i] = 2*left[i/2];
            }
        }
        return left[n];
}

然后DP需要的space太大了,所以这题适合用backtracking做

public int LastRemaining(int n) {
        return BackTracking(n,true);
    }
    private int BackTracking(int n, bool left)
    {
        if(n<=2) return left?n:1;
        if(n%2 == 0) return 2*BackTracking(n/2, !left)-(left?0:1);
        else  return 2*BackTracking(n/2,!left);
    }
时间: 2024-08-30 05:25:43

390. Elimination Game的相关文章

Leetcode 390. Elimination Game

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 ri

390 Elimination Game 淘汰游戏

详见:https://leetcode.com/problems/elimination-game/description/ C++: 方法一: class Solution { public: int lastRemaining(int n) { return help(n, true); } int help(int n, bool left2right) { if (n == 1) { return 1; } if (left2right) { return 2 * help(n / 2,

【leetcode】390. Elimination Game

题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] -> [2 6] -> [6]这个序列中,我们可以得到公差分别是1,2,4.如果我们把n扩大一点,打印出其中每一步剩余的数组序列,我们很容易发现公差是pow(2,n)次方,发现了这个规律后,一切就水到渠成了.接下来,我们只要记录每一次操作后剩下序列的low,high以及序列的长度,直到最后序列只有一

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

hdu 4975 A simple Gaussian elimination problem.(网络流,判断矩阵是否存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One day, he drew a table with several rows and columns, randomly wrote numbers on each elements of the table. Then he counted the sum of each row and col

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

8月11号=》386页-390页

14.9 navigator和地理位置 window对象有一个navigator属性,该属性对应于Navigator对象,该对象代表浏览该页面所使用的浏览器.该对象在不同 的平台上的信息并不完全相同,但总包含如下几个常用的属性. appName:返回该浏览器的内核名称. appVersion:返回该浏览器当前的版本号. platform:返回当前浏览器所在的操作系统. 14.9.1 HTML5新增的geolocation属性 HTML5为navigator新增了一个geolocation属性,这