-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
486.py
25 lines (22 loc) · 985 Bytes
/
486.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
__________________________________________________________________________________________________
sample 24 ms submission
class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n = len(nums)
if n==1 or n%2==0: return True
dp = [[0]*n for _ in range(n)]
for i in range(n-1, -1, -1):
for j in range(i+1, n):
dp[i][j] = max(nums[i]-dp[i+1][j], nums[j]-dp[i][j-1])
return dp[0][-1]>=0
__________________________________________________________________________________________________
sample 13192 kb submission
class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n = len(nums)
f = [0] * n
for r in range(1, n):
for l in range(r)[::-1]:
f[l] = max(nums[l] - f[l+1], nums[r] - f[l])
return f[0] >= 0
__________________________________________________________________________________________________