[leetcode] 486. Predict the Winner (medium)】的更多相关文章

原题 思路: 解法一: 转换比较拿取分数多少的思路,改为考虑 player拿的分数为正,把Player2拿的视为负,加上所有分数,如果最后结果大于0则Player1赢. 思考得出递归表达式: max(nums[beg] - player2(beg + 1, end), nums[end] - player2(beg, end + 1)) 此解法效率很差 104ms,beats 5.32% class Solution { public: bool PredictTheWinner(vector<…
lc 486 Predict the Winner 486 Predict the Winner 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…
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…
题目链接:https://leetcode.com/problems/predict-the-winner/ 1.暴力递归 当前数组左边界:i,右边界:j: 对于先发者来说,他能取到的最大值是:max(arr[i] + second(arr, i + 1, j), arr[j] + second(arr, i, j - 1)); (arr[i] + 作为后发者,在 i+1 到 j 上取得的值),(arr[j] + 作为后发者,在 i 到 j-1 上取得的值) 中大的一个. 对于后发者来说,他是被…
[LeetCode]486. Predict the Winner 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/predict-the-winner/description/ 题目描述: Given an array of scores that are non-negative integers…
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…
题目如下: 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 f…
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…
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数可取时游戏结束.最终获得分数总和最多的玩家获胜.给定一个表示分数的数组,预测玩家1是否会成为赢家.你可以假设每个玩家的玩法都会使他的分数最大化.示例 1:输入: [1, 5, 2]输出: False解释: 一开始,玩家1可以从1和2中进行选择.如果他选择2(或者1),那么玩家2可以从1(或者2)和5…
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数可取时游戏结束.最终获得分数总和最多的玩家获胜. 给定一个表示分数的数组,预测玩家1是否会成为赢家.你可以假设每个玩家的玩法都会使他的分数最大化. 示例 1: 输入: [1, 5, 2] 输出: Fa…
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat…
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一…
LeetCode 题解 593. Valid Square (Medium) 判断给定的四个点,是否可以组成一个正方形 https://leetcode.com/problems/valid-square/solution/ bug "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * @created 2020-11-11 * @modified * * @description 593…
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…
原题链接在这里:https://leetcode.com/problems/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. Ea…
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…
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in t…
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…
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…
2018-04-22 19:19:47 问题描述: 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 wil…
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a l…
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPL…
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向右或向下移动一格,直至右下角的格子.返回所有不同路径的总数. 注意:m和n都不超过100 3. 解题思路…
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开始数第k个结点之后的链表与之前的链表交换位置,例如 3. 解题思路 (1)首先要注意head结点不是指头结点,而是指第一个结点: (2)当head为null或者链表中只有一个结点时,返回head: (3)个人觉得题目出的很不友好,当k=链表的长度时,返回的时原链表:当k大于链表的长度时,则不是...…
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整数所有排列序列中的第k个序列,返回String类型的第k个序列 3. 解题思路 首先我们要知道这个序列是按照什么规律排列下去的,假如此时n=4,k= 21,n=4时所有的排列如下: 可以看出 n=4 时,一共有 4!=24种排列组合. 每一个数字开头各有 6 种排列组合,因此我们可以把同一数字开头的…
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域进行合并.Interval定义如下 例如下图中,[1, 3] 和 [2, 6]是有重叠部分的,可以合并成[1, 6] 3. 解题思路 先取第一个interval对象的 start 和 end 的值 ,然后对这个集合进行遍历.比较当前遍历对象的start是否比前一个对象的end小,小的话则说明二者存在…
1. 原题链接 https://leetcode.com/problems/jump-game/description/ 2. 题目要求 给定一个整型数组,数组中没有负数.从第一个元素开始,每个元素的值代表每一次你能从当前位置跳跃的步数.问能否跳到该数组的最后一个元素位置 注意:可以跳的步数超出数组长度依旧视为可以达到最后位置 3. 解题思路 从第一个元素开始遍历,记录下你所能到达的最远位置,例如{2, 2, 0, 1, 2},遍历第一个元素时,你所能到达的最远位置是“i+nums[i]”=2,…
1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后返回结果为 [1,2,3,6,9,8,7,4,5] 3. 解题思路 按照螺旋的顺序进行遍历,每一次遍历螺旋顺序里的一个圈,如下图每一种颜色代表一次遍历得到的结果 4. 代码实现 import java.util.ArrayList; import java.util.List; public cla…
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数组所有的排列组合 3. 解题思路 采用递归的方法,使用一个tempList用来暂存可能的排列. 4. 代码实现 import java.util.ArrayList; import java.util.List; public class Permutations46 { public static…
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计. 注意:不能使用乘法.除法和取余运算 3. 解题思路 陷阱一:MIN_VALUE/-1会溢出.因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1 陷阱二:除数divisor不能等于“0” 思路一:使用一个while循环…