国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 周至县| 惠水县| 大悟县| 荣成市| 铅山县| 富锦市| 乌什县| 沁阳市| 彭阳县| 武川县| 长泰县| 洪湖市| 府谷县| 威远县| 南川市| 玉门市| 南靖县| 桃江县| 海门市| 子长县| 南澳县| 阿拉善左旗| 清远市| 西城区| 赣州市| 曲靖市| 宜都市| 遂宁市| 伊金霍洛旗| 固始县| 文昌市| 江津市| 长泰县| 大同县| 唐海县| 英德市| 丹寨县| 湘西| 五原县| 孟津县| 花莲市|