486. Predict the Winner

Description

Given an array of scores that are non-negative integers. Player 1 picks one of the numbers from either end of the array followed by the player 2 and then player 1 and so on. Each time a player picks a number, that number will not be available for the next player. This continues until all the scores have been chosen. The player with the maximum score wins.

Given an array of scores, predict whether player 1 is the winner. You can assume each player plays to maximize his score.

Example 1:

Input: [1, 5, 2]
Output: False
Explanation: Initially, player 1 can choose between 1 and 2.
If he chooses 2 (or 1), then player 2 can choose from 1 (or 2) and 5. If player 2 chooses 5, then player 1 will be left with 1 (or 2).
So, final score of player 1 is 1 + 2 = 3, and player 2 is 5.
Hence, player 1 will never be the winner and you need to return False.

Example 2:

Input: [1, 5, 233, 7]
Output: True
Explanation: Player 1 first chooses 1. Then player 2 have to choose between 5 and 7. No matter which number player 2 choose, player 1 can choose 233.
Finally, player 1 has more score (234) than player 2 (12), so you need to return True representing player1 can win.

Note:

  1. 1 <= length of the array <= 20.
  2. Any scores in the given array are non-negative integers and will not exceed 10,000,000.
  3. If the scores of both players are equal, then player 1 is still the winner.

Solution

DP, O(n ^ 2), S(n ^ 2)

The idea behind the recursive approach is simple. The two players Player 1 and Player 2 will be taking turns alternately. For the Player 1 to be the winner, we need scorePlayer_1≥scorePlayer_2. Or in other terms, scorePlayer_1?scorePlayer_2≥0.

Thus, for the turn of Player 1, we can add its score obtained to the total score and for Player 2's turn, we can substract its score from the total score. At the end, we can check if the total score is greater than or equal to zero(equal score of both players), to predict that Player 1 will be the winner.
We can omit the use of turnturn to keep a track of the player for the current turn. To do so, we can make use of a simple observation. If the current turn belongs to, say Player 1, we pick up an element, say xx, from either end, and give the turn to Player 2. Thus, if we obtain the score for the remaining elements(leaving xx), this score, now belongs to Player 2. Thus, since Player 2 is competing against Player 1, this score should be subtracted from Player 1's current(local) score(xx) to obtain the effective score of Player 1 at the current instant.

class Solution {
    public boolean PredictTheWinner(int[] nums) {
        int n = nums.length;
        int[][] dp = new int[n][n];  // dp[i][j] = current player score - other player score starting from i and end with j
        
        for (int s = n - 1; s >= 0; --s) {
            for (int e = s; e < n; ++e) {
                int a = nums[s] - (s < e ? dp[s + 1][e] : 0);
                int b = nums[e] - (s < e ? dp[s][e - 1] : 0);
                dp[s][e] = Math.max(a, b);
            }
        }
        
        return dp[0][n - 1] >= 0;
    }
}

變種題:拿卡片游戲

每張卡片都有一個值,給定一堆卡片從一頭拿茂洒,每次可以拿一到三張照皆,兩人輪流拿豆励,求最高得分∶考慮卡片值為負的情況辣之。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末垢箕,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子呢撞,更是在濱河造成了極大的恐慌损姜,老刑警劉巖饰剥,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異摧阅,居然都是意外死亡汰蓉,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門棒卷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來顾孽,“玉大人,你說我怎么就攤上這事比规∪艉瘢” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵苞俘,是天一觀的道長盹沈。 經(jīng)常有香客問我,道長吃谣,這世上最難降的妖魔是什么乞封? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮岗憋,結果婚禮上肃晚,老公的妹妹穿的比我還像新娘。我一直安慰自己仔戈,他們只是感情好关串,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著监徘,像睡著了一般晋修。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上凰盔,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天墓卦,我揣著相機與錄音,去河邊找鬼户敬。 笑死落剪,一個胖子當著我的面吹牛,可吹牛的內容都是我干的尿庐。 我是一名探鬼主播忠怖,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼抄瑟!你這毒婦竟也來了凡泣?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎问麸,沒想到半個月后往衷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡严卖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年席舍,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哮笆。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡来颤,死狀恐怖,靈堂內的尸體忽然破棺而出稠肘,到底是詐尸還是另有隱情福铅,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布项阴,位于F島的核電站滑黔,受9級特大地震影響,放射性物質發(fā)生泄漏环揽。R本人自食惡果不足惜略荡,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望歉胶。 院中可真熱鬧汛兜,春花似錦、人聲如沸通今。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辫塌。三九已至漏策,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間臼氨,已是汗流浹背哟玷。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留一也,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓喉脖,卻偏偏與公主長得像椰苟,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子树叽,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內容