Skip to content

Commit 2a417d9

Browse files
author
hasibulislam999
committed
Predict the Winner problem solved
1 parent afaed26 commit 2a417d9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Game Theory/486_predict-the-winner.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
};

0 commit comments

Comments
 (0)