forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredict the Winner.py
26 lines (22 loc) · 932 Bytes
/
Predict the Winner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
dp = [[-1] * len(nums) for _ in nums]
def get_score(i: int, j: int) -> int:
if i == j:
dp[i][j] = 0
return dp[i][j]
if i == j - 1:
dp[i][j] = nums[j] if nums[i] > nums[j] else nums[i]
return dp[i][j]
if dp[i][j] != -1:
return dp[i][j]
y1 = get_score(i + 1, j - 1)
y2 = get_score(i + 2, j)
y3 = get_score(i, j - 2)
res_y1 = y1 + nums[j] if y1 + nums[j] > y2 + nums[i+1] else y2 + nums[i+1]
res_y2 = y1 + nums[i] if y1 + nums[i] > y3 + nums[j-1] else y3 + nums[j-1]
dp[i][j] = min(res_y1, res_y2)
return dp[i][j]
y = get_score(0, len(nums) - 1)
x = sum(nums) - y
return 0 if y > x else 1