成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode390.Elimination Game

Godtoy / 1001人閱讀

摘要:題目要求假設(shè)有一共個數(shù)字,從左往右開始每隔一位刪除一個數(shù)字,到達最右側(cè)后,再從右往左每隔一位刪除一個數(shù)字,如此反復(fù),直到剩下最后一個數(shù)字。由此可見,假如我們定義一個遞歸函數(shù)我們可以有來獲取結(jié)果。

題目要求
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

假設(shè)有1-n一共n個數(shù)字,從左往右開始每隔一位刪除一個數(shù)字,到達最右側(cè)后,再從右往左每隔一位刪除一個數(shù)字,如此反復(fù),直到剩下最后一個數(shù)字。問最后剩下的數(shù)字是多少。

思路一:遞歸

先從一個例子入手,當(dāng)n等于7時,數(shù)字序列為1,2,3,4,5,6,7, 刪除序列如下:

1 2 3 4 5 6 7
  2   4   6
      4

可以看到,第一輪刪除后剩下的2,4,6就相當(dāng)于是1,2,3的兩倍,我們可以等價于從右往左刪除1,2,3后剩余的結(jié)果乘2。由此可見,假如我們定義一個遞歸函數(shù)f(n, left),我們可以有f(n/2, right)來獲取結(jié)果。

    public int lastRemaining(int n) {
        return lastRemaining(n, true);
    }
    
    public int lastRemaining(int n, boolean left) {
        if(n == 1) {
            return 1;
        }
        if(n % 2 == 1) {
            return lastRemaining(n / 2, !left) * 2;
        }else{
            if( left ) {
                return lastRemaining(n/2, !left) * 2;
            }else {
                return lastRemaining(n/2, !left) * 2 -1;
            }
        }
    }
思路二

這里其實存在一個鏡像刪除的問題,也就是說,對于任何一個1~n的序列來說,從左往右開始刪除和從右往左開始刪除剩余的結(jié)果的和一定為(n+1),也就是所謂的鏡像刪除問題。
舉個例子:

從左往右開始刪除
1 2 3 4 5 6
  2   4   6
      4
      
從右往左開始刪除
1 2 3 4 5 6
1   3   5
    3 

可以看到二者剩余的值加起來一定為n+1即7。
根據(jù)這個原理,我們可以優(yōu)化上面的遞歸,無需再利用left值來標記是從左往右刪除還是從右往左刪除,直接執(zhí)行鏡像刪除即可。

    public int lastRemaining2(int n) {
        return n == 1 ? 1 : (1 + n / 2 - lastRemaining2(n/2)) * 2;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/77452.html

相關(guān)文章

  • [Leetcode] Nim Game 尼姆游戲

    摘要:腦筋急轉(zhuǎn)彎復(fù)雜度時間空間思路這題往小說可以追溯到小學(xué)奧數(shù)或者腦筋急轉(zhuǎn)彎的書中,往大說可以深究到博弈論。代碼如果一開始就是的倍數(shù),你就輸了,因為對方可以用同樣的策略 Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each ...

    cartoon 評論0 收藏0
  • LeetCode[45] Jump Game II

    摘要:復(fù)雜度思路每次設(shè)置一個窗口,觀察在這一步下能到達的最遠距離,不斷的移動這個窗口。計數(shù),需要移動幾次,才能覆蓋到末尾的值。 LeetCode[45] Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Eac...

    keelii 評論0 收藏0
  • Leetcode PHP題解--D37 682. Baseball Game

    摘要:題目鏈接題目分析給定一個字符串?dāng)?shù)組,每一個字符串有以下形式數(shù)字。直接計算得分。。代表上一輪分數(shù)無效。思路這題沒什么好說的了。用區(qū)分各種情況,進行相應(yīng)處理即可。最終代碼若覺得本文章對你有用,歡迎用愛發(fā)電資助。 682. Baseball Game 題目鏈接 682. Baseball Game 題目分析 給定一個字符串?dāng)?shù)組,每一個字符串有以下形式: 數(shù)字。直接計算得分。 +。代表本輪...

    wzyplus 評論0 收藏0
  • Leetcode PHP題解--D64 292. Nim Game

    摘要:拿到最后一顆石頭的一方為剩方?,F(xiàn)給定一個石頭數(shù)量,判斷你最終是否能取得勝利。對方全拿,對方贏。因此,必輸無疑。當(dāng)剩下的石頭為的整數(shù)倍雙方都采取最優(yōu)策略時,先下手的一方為輸家。因此這個題目就很簡單了,只要判斷給定的數(shù)字是否是的整數(shù)倍即可。 D64 292. Nim Game 題目鏈接 292. Nim Game 題目分析 假設(shè)你和朋友玩一個撿石頭的游戲,你和朋友輪流拿1~3顆石頭。拿到最...

    XGBCCC 評論0 收藏0
  • [Leetcode] Jump Game 跳躍游戲

    摘要:代碼記錄下當(dāng)前區(qū)域的上界,以便待會更新下一個區(qū)域的上界更新下一個區(qū)域的上界更新下一個區(qū)域的下界后續(xù)如果要求返回最短跳躍路徑,如何實現(xiàn)可以使用,并根據(jù)一個全局最短步數(shù)維護一個全局最短路徑,當(dāng)搜索完所有可能后返回這個全局最短路徑。 Jump Game I 最新解法請見:https://yanjia.me/zh/2019/01/... Given an array of non-negat...

    venmos 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<