国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Leetcode 486. Predict the Winner 預(yù)測贏家 解題報(bào)告

2019-11-08 02:53:13
字體:
供稿:網(wǎng)友

1 解題思想

開學(xué)了,我又回歸了。 這道題的意思是給了一個數(shù)組,兩個人玩,每個人可以在當(dāng)前的數(shù)組的頭部或者尾部選擇一個數(shù),作為自己的分?jǐn)?shù),然后換人,已經(jīng)被選過的數(shù)字不能再用。 現(xiàn)在有兩個人玩,Player1和Player2,問給了當(dāng)前的數(shù)組后,Player1能不能保證勝利?

解題方法,就是需要做一個決策,看Player1有沒有一個必勝的選擇決策,這個只需要遞歸下去選就好。 所謂的必勝,就是當(dāng)前選到的分?jǐn)?shù),大于對手能選擇到的。

2 原題

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: FalseExplanation: 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: TrueExplanation: 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 <= length of the array <= 20. Any scores in the given array are non-negative integers and will not exceed 10,000,000.If the scores of both players are equal, then player 1 is still the winner.

3 AC解

public class Solution { public boolean PredictTheWinner(int[] nums) { return helper(nums, 0, nums.length-1)>=0; } private int helper(int[] nums, int start, int end){ if(start==end){ return nums[start]; } else{ return Math.max(nums[start] - helper(nums,start+1,end),nums[end] - helper(nums,start,end-1)); } }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉安县| 宜都市| 吉林省| 宜州市| 汕尾市| 大田县| 汉沽区| 林芝县| 福清市| 嘉祥县| 会东县| 田阳县| 乐东| 鸡东县| 屏东市| 贺州市| 吴桥县| 玉田县| 建瓯市| 米林县| 肥城市| 天全县| 当雄县| 合水县| 景德镇市| 浦县| 红河县| 旬阳县| 扎兰屯市| 奉化市| 新建县| 凤山市| 天柱县| 登封市| 琼结县| 五莲县| 桐柏县| 贵南县| 阿鲁科尔沁旗| 舒兰市| 望江县|