File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Title: Predict the Winner
3
+ * Description: You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2.
4
+ * Author: Hasibul Islam
5
+ * Date: 06/05/2023
6
+ */
7
+
8
+ /**
9
+ * @param {number[] } nums
10
+ * @return {boolean }
11
+ */
12
+ var PredictTheWinner = function ( nums ) {
13
+ const n = nums . length ;
14
+ const dp = [ ] ;
15
+
16
+ for ( let i = 0 ; i < n ; i ++ ) {
17
+ dp [ i ] = new Array ( n ) . fill ( 0 ) ;
18
+ dp [ i ] [ i ] = nums [ i ] ;
19
+ }
20
+
21
+ for ( let len = 2 ; len <= n ; len ++ ) {
22
+ for ( let start = 0 ; start < n - len + 1 ; start ++ ) {
23
+ const end = start + len - 1 ;
24
+ dp [ start ] [ end ] = Math . max (
25
+ nums [ start ] - dp [ start + 1 ] [ end ] ,
26
+ nums [ end ] - dp [ start ] [ end - 1 ]
27
+ ) ;
28
+ }
29
+ }
30
+
31
+ return dp [ 0 ] [ n - 1 ] >= 0 ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments