File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 280
280
476|[ Number Complement] ( ./0476-number-complement.js ) |Easy|
281
281
482|[ License Key Formatting] ( ./0482-license-key-formatting.js ) |Easy|
282
282
485|[ Max Consecutive Ones] ( ./0485-max-consecutive-ones.js ) |Easy|
283
+ 486|[ Predict the Winner] ( ./0486-predict-the-winner.js ) |Medium|
283
284
491|[ Non-decreasing Subsequences] ( ./0491-non-decreasing-subsequences.js ) |Medium|
284
285
492|[ Construct the Rectangle] ( ./0492-construct-the-rectangle.js ) |Easy|
285
286
496|[ Next Greater Element I] ( ./0496-next-greater-element-i.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 486. Predict the Winner
3
+ * https://leetcode.com/problems/predict-the-winner/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given an integer array nums. Two players are playing a game with this array:
7
+ * player 1 and player 2.
8
+ *
9
+ * Player 1 and player 2 take turns, with player 1 starting first. Both players start
10
+ * the game with a score of 0. At each turn, the player takes one of the numbers from
11
+ * either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the
12
+ * size of the array by 1. The player adds the chosen number to their score. The game
13
+ * ends when there are no more elements in the array.
14
+ *
15
+ * Return true if Player 1 can win the game. If the scores of both players are equal,
16
+ * then player 1 is still the winner, and you should also return true. You may assume
17
+ * that both players are playing optimally.
18
+ */
19
+
20
+ /**
21
+ * @param {number[] } nums
22
+ * @return {boolean }
23
+ */
24
+ var predictTheWinner = function ( nums ) {
25
+ const diff = new Array ( nums . length ) . fill ( 0 ) ;
26
+
27
+ for ( let i = nums . length - 1 ; i >= 0 ; i -- ) {
28
+ diff [ i ] = nums [ i ] ;
29
+ for ( let j = i + 1 ; j < nums . length ; j ++ ) {
30
+ diff [ j ] = Math . max ( nums [ i ] - diff [ j ] , nums [ j ] - diff [ j - 1 ] ) ;
31
+ }
32
+ }
33
+
34
+ return diff [ nums . length - 1 ] >= 0 ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments