inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles):
N = len(piles)
@lru_cache(None)
def dp(i, j):
# The value of the game [piles[i], piles[i+1], ..., piles[j]].
if i > j: return 0
parity = (j - i) % 2
if parity == 1: # first player
return max(piles[i] + dp(i+1,j), piles[j] + dp(i,j-1))
else:
return min(-piles[i] + dp(i+1,j), -piles[j] + dp(i,j-1))
return dp(0, N - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles):
def dp(piles, l, r, memo):
if l > r:
return 0
if (l,r) in memo:
return memo[(l,r)]
op1 = piles[l] - dp(piles, l+1, r, memo)
op2 = piles[r] - dp(piles, l, r-1, memo)
stone = max(op1, op2)
memo[(l,r)] = stone
return stone
return dp(piles, 0, len(piles)-1, {}) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def dfs(i, j, isAlex):
if j < i: return 0
elif i == j: return piles[i] if isAlex else -piles[i]
if isAlex:
return max(piles[i]+dfs(i+1, j, not isAlex), piles[j]+dfs(i, j-1, not isAlex))
else:
return min(-piles[i]+dfs(i+1, j, not isAlex), -piles[j]+dfs(i, j-1, not isAlex))
if not piles: return False
n = len(piles)
return dfs(0, n-1, True) > 0
def stoneGame(self, piles: List[int]) -> bool:
if not piles: return False
n = len(piles)
nums = piles
dp = [[[None, None] for _ in range(n)] for _ in range(n)]
for i in range(n):
dp[i][i][0] = -nums[i]
dp[i][i][1] = nums[i]
for l in range(1, n):
for i in range(n-l):
j = i+l
dp[i][j][0] = min(-nums[i]+dp[i+1][j][1], -nums[j]+dp[i][j-1][1])
dp[i][j][1] = max(nums[i]+dp[i+1][j][0], nums[j]+dp[i][j-1][0])
return dp[0][n-1][1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
self.memo = [[float('-inf') for _ in range(n)] for _ in range(n)]
return self.dfs(piles, 0, n - 1) > 0
def dfs(self, piles, left, right):
if left == right:
return piles[left]
if self.memo[left][right] == float('-inf'):
self.memo[left][right] = max(piles[left] - self.dfs(piles, left + 1, right), piles[right] - self.dfs(piles, left, right - 1))
return self.memo[left][right]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memo = dict()
return self.find(piles, 0, 0, True, 0, len(piles)-1, memo)
def find(self, piles, a_score, b_scord, is_a, front, end, memo):
if front > end:
return a_score > b_scord
key = (front, end)
if key in memo:
return memo[key]
if is_a:
res = self.find(piles, a_score + piles[front], b_scord, not is_a, front + 1, end, memo) or self.find(piles, a_score + piles[end], b_scord, not is_a, front, end-1, memo)
else:
res = self.find(piles, a_score, b_scord + piles[front], not is_a, front + 1, end, memo) or self.find(piles, a_score, b_scord + piles[end], not is_a, front, end-1, memo)
memo[key] = res
return res
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
return self.dfs(piles, 0, len(piles) - 1, {})
def dfs(self, piles, start, end, memo):
if start > end:
return 0
elif start == end:
return piles[start]
elif (start, end) in memo:
return memo[(start, end)]
# choose one which is most optimal for the player
L = piles[start] - self.dfs(piles, start + 1, end, memo)
R = piles[end] - self.dfs(piles, start, end - 1, memo)
memo[(start, end)] = max(L, R)
return memo[(start, end)]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def dfs(i, j):
if i == j:
return piles[i]
return max(piles[i] - dfs(i + 1, j), piles[j] - dfs(i, j - 1))
return dfs(0, len(piles) - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
from functools import lru_cache
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
@lru_cache(None)
def dp(i, j, asum, lsum, aturn):
if i > j:
return (0, 0)
if aturn:
asum += max(piles[i] + dp(i+1, j, asum, lsum, False)[0], piles[j] + dp(i, j-1, asum, lsum, False)[0])
else:
lsum += max(piles[i] + dp(i+1, j, asum, lsum, False)[1], piles[j] + dp(i, j-1, asum, lsum, False)[1])
return (asum, lsum)
asum, lsum = dp(0, n-1, 0, 0, True)
return bool(asum - lsum)
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles):
N = len(piles)
@lru_cache(None)
def dp(i, j):
# The value of the game [piles[i], piles[i+1], ..., piles[j]].
if i > j: return 0
parity = (j - i - N) % 2
if parity == 1: # first player
return max(piles[i] + dp(i+1,j), piles[j] + dp(i,j-1))
else:
return min(-piles[i] + dp(i+1,j), -piles[j] + dp(i,j-1))
return dp(0, N - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def dp(i, j):
if i + 1 == j:
arr = piles[i + 1: j + 1]
return max(arr) - min(arr)
L, R = piles[i], piles[j]
return max(L - R + dp(i + 1, j), R - L + dp(i, j - 1))
return dp(0, len(piles) - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
self.f_map, self.s_map = dict(), dict()
_sum = sum(piles)
alex = self.f(piles, 0, len(piles)-1)
return alex > _sum/2
def f(self, piles, start, end):
if start == end:
return piles[start]
if ((start, end) not in self.f_map):
v_val = max(piles[start] + self.s(piles, start + 1, end), piles[end] + self.s(piles, start, end-1))
self.f_map[(start, end)] = v_val
return self.f_map[(start, end)]
def s(self, piles, start, end):
if start == end:
return piles[start]
if ((start, end) not in self.s_map):
v_val = max(piles[start] + self.f(piles, start + 1, end), piles[end] + self.f(piles, start, end-1))
self.s_map[(start, end)] = v_val
return self.s_map[(start, end)]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
if len(piles) == 0:
return False
ans = []
l = len(piles)
for i in range(l):
ans.append(max(piles[0], piles[-1]))
piles.remove(max(piles[0], piles[-1]))
print(ans)
if ans[-2]>=ans[-1]:
return True
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def dp(i, j):
if i > j:
return 0
term = (j - i) % 2
if term == 1:
return max(dp(i + 1, j) + piles[i] , dp(i, j -1) + piles[j])
else:
return min(dp(i + 1, j) - piles[i] , dp(i, j - 1) - piles[j])
return dp(0, len(piles) - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
@lru_cache(None)
def sumRange(i, j):
if i == j: return piles[i]
else: return piles[i] + sumRange(i + 1, j)
@lru_cache(None)
def dp(i, j):
if i == j:
return piles[i]
else:
return max(piles[i] + sumRange(i+1, j) - dp(i + 1, j),
piles[j] + sumRange(i, j - 1) - dp(i, j - 1))
Alex = dp(0, n-1)
Lee = sumRange(0, n-1) - Alex
return Alex > Lee
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
cache = {}
def firstscore(i, j):
if i >= j:
return 0
# if j == i+1 and j < len(piles): #because [i, j)
# return piles[i]
if (i, j) in cache:
return cache[i, j]
res = max(piles[i] + min(firstscore(i+2, j), firstscore(i+1, j-1)),
piles[j - 1] + min(firstscore(i+1, j-1), firstscore(i, j-2)))
cache[i, j] = res
return cache[i, j]
Alex = firstscore(0, len(piles))
Lee = sum(piles) - Alex
return Alex > Lee
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
import numpy as np
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = np.empty((n,n))
for j in range(1,n):
dp[j-1,j] = abs(piles[j-1]-piles[j])
for j in range(3,n,2):
for i in range(0,n-j):
#want to get dp[i,i+j]
#crrespond dp[i,i+j-2] dp[i+1,i+j-1],dp[i-2,i+j]
temp1 = piles[i+j]+min(-piles[i+j-1]+dp[i,i+j-2],-piles[i]+dp[i+1,i+j-1])
temp2 = piles[i]+min(-piles[i+1]+dp[i+2,i+j],-piles[i+j]+dp[i+1,i+j-1])
dp[i,i+j] = max(temp1,temp2)
if dp[0,n-1]>0:
return True
else: return False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = {}
def dfs(i, j, Alex, Lee, who):
if (i, j) not in dp:
if i > j:
return Alex, Lee
if who:
Alextmp1, Leetmp1 = dfs(i + 1, j - 0, Alex + piles[i], Lee, not who)
Alextmp2, Leetmp2 = dfs(i + 0, j - 1, Alex + piles[j], Lee, not who)
if Alextmp1 - Alex > Alextmp2 - Alex:
dp[i, j] = Alextmp1 - Alex, Leetmp1 - Lee
else:
dp[i, j] = Alextmp2 - Alex, Leetmp2 - Lee
else:
Alextmp1, Leetmp1 = dfs(i + 1, j - 0, Alex, Lee + piles[i], not who)
Alextmp2, Leetmp2 = dfs(i + 0, j - 1, Alex, Lee + piles[j], not who)
if Leetmp1 - Lee > Leetmp2 - Lee:
dp[i, j] = Alextmp1 - Alex, Leetmp1 - Lee
else:
dp[i, j] = Alextmp2 - Alex, Leetmp2 - Lee
return Alex + dp[i, j][0], Lee + dp[i, j][1]
Alex, Lee = dfs(0, len(piles) - 1, 0, 0, True)
#print(Alex, Lee)
if Alex > Lee:
return True
else:
return False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
self.result_cache = {}
def f(low, high, nums):
if low > high:
return 0
if low == high:
return nums[low]
if (low, high) in self.result_cache:
return self.result_cache[(low, high)]
l = nums[low] + min(f(low + 2, high, nums), f(low + 1, high - 1 , nums))
h = nums[high] + min(f(low + 1, high - 1, nums), f(low, high - 2, nums))
r = max(l, h)
self.result_cache[(low, high)] = r
return r
p1 = f(0, len(piles) - 1, piles)
p2 = sum(piles) - p1
return p1 > p2
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def dp(self, i, j):
if (i, j) in self.memo:
return self.memo[(i, j)]
if i > j:
return (0, 0)
firstTakeLeft, firstTakeRight = self.piles[i] + self.dp(i+1, j)[1], self.piles[j] + self.dp(i, j-1)[1]
if firstTakeLeft > firstTakeRight:
self.memo[(i, j)] = (firstTakeLeft, self.dp(i+1,j)[0])
else:
self.memo[(i, j)] = (firstTakeRight, self.dp(i, j-1)[0])
return self.memo[(i, j)]
def stoneGame(self, piles: List[int]) -> bool:
self.piles = piles
self.memo = {}
for i in range(len(piles)):
self.memo[(i, i)] = (piles[i], 0)
fir, sec = self.dp(0, len(piles)-1)
return fir >= sec
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memo = [[-1 for _ in range(len(piles))] for _ in range(len(piles))]
return self.recursive(0, 0, len(piles) - 1, 0, 0, piles, memo)
def recursive(self, turn, left_idx, right_idx, sum1, sum2, piles, memo):
# Base Case
if left_idx >= right_idx:
return sum1 > sum2
if memo[left_idx][right_idx] != -1:
return memo[left_idx][right_idx]
if turn % 2 == 0:
res = self.recursive(turn + 1, left_idx + 1, right_idx, sum1 + piles[left_idx], sum2, piles, memo) or self.recursive(turn + 1, left_idx, right_idx - 1, sum1 + piles[right_idx], sum2, piles, memo)
else:
res = self.recursive(turn + 1, left_idx + 1, right_idx, sum1, sum2 + piles[left_idx], piles, memo) or self.recursive(turn + 1, left_idx, right_idx - 1, sum1, sum2 + piles[right_idx], piles, memo)
memo[left_idx][right_idx] = res
return memo[left_idx][right_idx]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memo = {}
def netWin(left, right):
if (left, right) not in memo:
if left > right:
return 0
memo[(left, right)] = -float('inf')
memo[(left, right)] = max(memo[(left, right)], piles[left] - netWin(left + 1, right))
memo[(left, right)] = max(memo[(left, right)], piles[right] - netWin(left, right - 1))
return memo[(left, right)]
res = netWin(0, len(piles) - 1)
# print (memo, res)
return True if res > 0 else False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
@lru_cache(None)
# dp(i, j) max net score alex can get
def dp(i, j):
if i > j:
return 0
if (n - j + i) % 2 == 1: # alex plays
return max(piles[i] + dp(i + 1, j), piles[j] + dp(i, j - 1))
return min(dp(i + 1, j) - piles[i], dp(i, j - 1) - piles[j])
return dp(0, n - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memo = {}
def netWin(left, right):
if (left, right) not in memo:
if left > right:
return 0
memo[(left, right)] = -float('inf')
memo[(left, right)] = max(memo[(left, right)], piles[left] - netWin(left + 1, right))
memo[(left, right)] = max(memo[(left, right)], piles[right] - netWin(left, right - 1))
return memo[(left, right)]
res = netWin(0, len(piles) - 1)
# print (memo, res)
return True if res > 0 else False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = [[[0 for i in range(2)] for z in range(len(piles))] for j in range(len(piles))]
for i in range(len(dp)):
dp[i][i] = [piles[i], 0]
for i in range(len(dp)-2, -1, -1):
for j in range(i+1, len(dp[0])):
left = piles[i] + dp[i+1][j][1]
right = piles[j] + dp[i][j-1][1]
if left > right:
dp[i][j][0] = left
dp[i][j][1] = dp[i+1][j][0]
else:
dp[i][j][0] = right
dp[i][j][1] = dp[i][j-1][0]
res = dp[0][-1]
if dp[0][-1][0] - dp[0][-1][1] > 0:
return True
else:
return False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
l = len(piles)
dp = [[-1 for i in range(l)] for j in range(l)]
def solve(piles, i, j, tot):
alex = (j-i-l) % 2
if i == j:
if alex:
tot += piles[i]
return tot > 0
if dp[i][j] != -1:
return dp[i][j]
if alex:
if solve(piles, i+1, j, tot+piles[i]):
dp[i][j] = True
return True
dp[i][j]= solve(piles, i, j-1, tot+piles[j])
return dp[i][j]
else:
if solve(piles, i+1, j, tot-piles[i]):
dp[i][j] = True
return True
dp[i][j]= solve(piles, i, j-1, tot-piles[j])
return dp[i][j]
return solve(piles, 0, l-1, 0)
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
table = [[False] * (n+1) for _ in range(n+1)]
for i in range(n+1):
table[i][i] = True
for row in range(n-1, -1, -1):
for col in range(row, n+1):
if table[row][col-1] == False:
table[row][col] = True
if table[row+1][col] == False:
table[row][col] = True
return table[0][n]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
self.dp = [[None for i in range(len(piles))] for i in range(len(piles))]
self.func(piles, 0, 0, len(piles)-1, 0, 0)
return self.dp[0][len(piles)-1]
def func(self, arr, turn, i, j, a, b):
if self.dp[i][j] == True:
return True
elif self.dp[i][j] == False:
return False
if i == j:
b += arr[i]
self.dp[i][j] = a > b
return self.dp[i][j]
if not turn:
self.dp[i][j] = self.func(arr, turn^1, i+1, j, a+arr[i], b) or self.func(arr, turn^1, i, j-1, a+arr[j], b)
return self.dp[i][j]
else:
self.dp[i][j] = self.func(arr, turn^1, i+1, j, a, b+arr[i]) or self.func(arr, turn^1, i, j-1, a, b+arr[j])
return self.dp[i][j]
# Gives TLE
# def recur(self, arr, turn, a, b):
# if len(arr) == 1:
# b += arr[0]
# return a > b
# if not turn:
# return self.recur(arr[1:], turn^1, a+arr[0], b) or self.recur(arr[:-1], turn^1, a+arr[-1], b)
# else:
# return self.recur(arr[1:], turn^1, a, b+arr[0]) or self.recur(arr[:-1], turn^1, a, b+arr[-1])
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = piles.copy()
for i in range(len(piles)-2, -1, -1):
for j in range(i+1, len(piles)):
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j - 1])
return dp[len(dp) - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [0] * n
for i in range(n):
for j in range(i, -1 , -1):
if i == j:
dp[i] = piles[i]
else:
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j+1])
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [piles[0]] * n
for j in range(1, n):
dp[j] = piles[j]
for i in range(j-1, -1 ,-1):
dp[i] = max(piles[i] - dp[i+1], piles[j] - dp[i])
#print(dp)
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = piles[:]
for d in range(1, n):
for i in range(n - d):
dp[i] = max(piles[i] - dp[i + 1], piles[i + d] - dp[i])
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [0] * n
# i is rightbound
for i in range(n):
# j is leftbound
for j in range(i, -1, -1):
if i == j:
dp[i] = piles[i]
else:
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j+1])
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# Let f(i,j) be the maximum difference Alex-Lee
# Key idea: If Alex takes pile[i], then Lee will play optimally to get a score of f(i+1, j) for him,
# which is equilvalent to Alex getting a score of -f(i+1, j). So Alex's total score is pile[i] - f(i+1,j)
# Then just take the pile that results in maximum score.
# f(i,j) = max(piles[i] - f(i+1,j), piles[j] - f(i,j-1))
# Base case: f(i,i) = piles[i]
# If you draw the 2d grid, it looks like filling in diagonal order
# Can do space optimization using 1D DP
n = len(piles)
if n == 1:
return True
dp = piles[:] # Base row
for l in range(1, n):
for i in range(n-l-1, -1, -1):
j = i+l
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j-1])
return dp[-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
payoffs = piles
r = 1
while len(payoffs) > 1:
new_payoffs = []
for i in range(len(payoffs) - 1):
new_payoffs.append(max(piles[i] - payoffs[i+1], piles[i + r] - payoffs[i]))
payoffs = new_payoffs
r += 1
return payoffs[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = piles[:]
for d in range(1,n):
for i in range(n-d):
dp[i] = max(piles[i]-dp[i+1], piles[i+d]-dp[i])
return dp[0]>0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
pre_s = [0]
acc = 0
for num in piles:
acc += num
pre_s.append(acc)
m = len(piles)
dp = [[0] * m for _ in range(m)]
for i in reversed(list(range(m))):
for j in range(i, m):
if i == j:
dp[i][j] = piles[i]
else:
s = pre_s[j+1] - pre_s[i]
if dp[i+1][j] < dp[i][j-1]:
dp[i][j] = s - dp[i+1][j]
else:
dp[i][j] = s - dp[i][j-1]
return dp[0][-1] > dp[1][-1] or dp[0][-1] > dp[0][-2]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
memo = piles[:]
for l in range(2, n+1):
for i in range(n+1-l):
memo[i] = max(piles[i] - memo[i+1], piles[i+l-1] - memo[i])
return memo[0]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# Mathematical
# If Alex takes the first pile initially, she can always take the third pile. If she takes the fourth pile initially, she can always take the second pile. At least one of first + third, second + fourth is larger, so she can always win.
# O(1)
# return True
# Dynamic Programming
# Time complexity: O(N^2)
# Space complexity: O(N^2)
# N = len(piles)
# @lru_cache(None)
# def dp(i, j):
# if i > j: return 0
# parity = (j - i - N) % 2
# if parity == 1: # first player
# return max(piles[i] + dp(i + 1, j), piles[j] + dp(i, j - 1))
# else:
# return min(-piles[i] + dp(i + 1, j), -piles[j] + dp(i, j - 1))
# return dp(0, N - 1) > 0
# N = len(piles)
# dp = [[0] * N for i in range(N)]
# for i in range(N):
# dp[i][i] = piles[i]
# for d in range(1, N):
# for i in range(N - d):
# dp[i][i + d] = max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1])
# return dp[0][-1] > 0
N = len(piles)
dp = piles[:]
for d in range(1, N):
for i in range(N - d):
dp[i] = max(piles[i] - dp[i + 1], piles[i + d] - dp[i])
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
N = len(piles)
dp = [[0] * N for _ in range(N)]
for i in range(N):
dp[i][i] = piles[i]
for i in range(N):
for j in range(i + 1, N):
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[i][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
# Init
for i in range(n):
dp[i][i] = piles[i]
for i in range(n-1, -1, -1):
for j in range(i+1, n):
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, nums: List[int]) -> bool:
n, f = len(nums), [i for i in nums]
for i in range(1, n):
for j in range(n-i):
f[j] = max(nums[j]-f[j+1], nums[j+i]-f[j])
return f[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# condition on first dp[i,j] is the number of stones greater than the opponent if both using the optimum strate
# take first, dp[0][n-1] = max(piles[0] - dp[1][n-1], piles[n-1] - dp[0][n-2])
# dp[i][j] = max(piles[i]-dp[i+1][j], piles[j]-dp[i][j+1]
# return dp[0][n-1]
n = len(piles)
dp = [[0]*n for _ in range(n)]
# initialize
for i in range(n):
dp[i][i] = piles[i]
for l in range(1, n):
for i in range(n-l):
j = i + l
dp[i][j] = max(piles[i]-dp[i+1][j], piles[j]-dp[i][j-1])
# print(dp)
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
N = len(piles)
dp = [[0] * N for _ in range(N)]
for i in range(N):
dp[i][i] = piles[i]
# j - i = d start from the shortest subarray
for d in range(1, N):
for i in range(N - d):
j = d + i
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0]*n for _ in range(n)]
for i in range(n): dp[i][i] = piles[i]
for l in range(1, n):
for i in range(n-l):
j = i + l
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for k in range(1, n):
for i in range(n-k):
j = i + k
# print(i, j, k)
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
f = [0] * n
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
f[j] = max(piles[i] - f[j], piles[j] - f[j - 1])
return f[n - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for d in range(1, n):
for i in range(n - d):
j = i + d
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# Let f(i,j) be the maximum difference Alex-Lee
# Key idea: If Alex takes pile[i], then Lee will play optimally to get a score of f(i+1, j) for him,
# which is equilvalent to Alex getting a score of -f(i+1, j). So Alex's total score is pile[i] - f(i+1,j)
# Then just take the pile that results in maximum score.
# f(i,j) = max(piles[i] - f(i+1,j), piles[j] - f(i,j-1))
# Base case: f(i,i) = piles[i]
# If you draw the 2d grid, it looks like filling in diagonal order
# Can do space optimization using 1D DP
n = len(piles)
if n == 1:
return True
# First let's do 2D DP, space optimize later
dp = [[None]*n for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for l in range(1, n):
for i in range(n-l):
j = i+l
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles):
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for d in range(1, n):
for i in range(n-d):
dp[i][i+d] = max(piles[i] - dp[i+1][i+d], piles[i+d] - dp[i][i+d-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# dp[i][j] represents for piles[i:j + 1], the maximum differnce of Alex - Lee
n = len(piles)
dp = [[0 for col in range(n)] for row in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for i in range(n - 1, -1 , -1):
for j in range(i + 1, n):
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[0][n - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
# 877. Stone Game
# It's tricky when we have even number of piles of stones.
# You may not have this condition in an interview.
# Follow-up:
# What if piles.length can be odd?
# What if we want to know exactly the diffenerce of score?
# Then we need to solve it with DP.
# dp[i][j] means the biggest number of stones
# you can get more than opponent picking piles in piles[i] ~ piles[j].
# You can first pick piles[i] or piles[j].
# If you pick piles[i], your result will be piles[i] - dp[i + 1][j]
# If you pick piles[j], your result will be piles[j] - dp[i][j - 1]
# So we get:
# dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
# We start from smaller subarray and then we use that to calculate bigger subarray.
# Note that take evens or take odds, it's just an easy strategy
# to win when the number of stones is even.
# It's not the best solution!
# I didn't find a tricky solution when the number of stones is odd (maybe there is).
class Solution1: # 2D DP
def stoneGame(self, piles: List[int]) -> bool:
p = piles
n = len(p)
dp = [[0] * n for i in range(n)]
for i in range(n):
dp[i][i] = p[i]
for d in range(1, n):
for i in range(n - d):
dp[i][i + d] = max(p[i] - dp[i + 1][i + d], p[i + d] - dp[i][i + d - 1])
return dp[0][-1] > 0
class Solution: # 1D DP, Space: O(N)
def stoneGame(self, p):
n = len(p)
dp = p[:]
for d in range(1, n):
for i in range(n - d):
# 这次拿完会赢,下次????
dp[i] = max(p[i] - dp[i + 1], p[i + d] - dp[i])
return dp[0] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i in range(n): dp[i][i] = piles[i]
s = sum(piles)
for l in range(1, n):
for i in range(n - l):
j = i + l
dp[i][j] = max(piles[i] + s - dp[i+1][j], piles[j] + s - dp[i][j-1])
return dp[0][n - 1] > (s // 2)
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, p):
n = len(p)
dp = [[0] * n for i in range(n)]
for i in range(n): dp[i][i] = p[i]
for d in range(1, n):
for i in range(n - d):
dp[i][i + d] = max(p[i] - dp[i + 1][i + d], p[i + d] - dp[i][i + d - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
if len(piles) == 0:
return False
n = len(piles)
dp = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for left in range(n):
for diff in range(1, n-left):
right = left + diff
dp[left][right] = max(piles[left] - dp[left+1][right], piles[right] - dp[left][right])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
if len(piles) == 2:
return True
alex = 0
lee = 0
i = 0
while(i < len(piles)):
if i%2 == 0:
alex += max(piles[0],piles[len(piles)-1])
piles.remove(max(piles[0],piles[len(piles)-1]))
else:
lee += min(piles[0],piles[len(piles)-1])
piles.remove(min(piles[0],piles(len(piles)-1)))
if alex>lee:
return True
return False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
def max_possible_score(i, j):
if j - i == 1:
return abs(piles[i] - piles[j])
return max(piles[i] - max_possible_score(i+1, j), piles[j] - max_possible_score(i, j-1))
#return max_possible_score(0, len(piles) - 1) > 0
n = len(piles)
dp = [[None for _ in range(n)] for _ in range(n)]
for i in range(n - 1):
j = i + 1
dp[i][j] = abs(piles[i] - piles[j])
for l in range(2, n):
for i in range(n - l):
j = i + l
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[None for _ in range(n)] for _ in range(n)]
for i in range(n - 1):
j = i + 1
dp[i][j] = abs(piles[i] - piles[j])
for l in range(2, n):
for i in range(n - l):
j = i + l
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
prefix = [0]
for p in piles:
prefix.append(prefix[-1] + p)
n = len(piles)
dp = [0] * n
dp[n - 1] = piles[n - 1]
for i in range(n - 2, -1, -1):
for j in range(i, n):
a = piles[i] + prefix[j + 1] - prefix[i + 1] - dp[j]
b = piles[j] + prefix[j] - prefix[i] - dp[j - 1]
dp[j] = max(a, b)
return dp[n - 1] > prefix[-1] // 2
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, p: List[int]) -> bool:
n = len(p)
dp = [[0]*n for _ in range(n)]
for i in range(n):
dp[i][i] = p[i]
for d in range(1, n):
for i in range(n - d):
dp[i][i+d] = max(p[i]-dp[i+1][i+d], p[i+d]-dp[i][i+d-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# Mathematical
# If Alex takes the first pile initially, she can always take the third pile. If she takes the fourth pile initially, she can always take the second pile. At least one of first + third, second + fourth is larger, so she can always win.
# O(1)
# return True
# Dynamic Programming
# Time complexity: O(N^2)
# Space complexity: O(N^2)
# N = len(piles)
# @lru_cache(None)
# def dp(i, j):
# if i > j: return 0
# parity = (j - i - N) % 2
# if parity == 1: # first player
# return max(piles[i] + dp(i + 1, j), piles[j] + dp(i, j - 1))
# else:
# return min(-piles[i] + dp(i + 1, j), -piles[j] + dp(i, j - 1))
# return dp(0, N - 1) > 0
N = len(piles)
dp = [[0] * N for i in range(N)]
for i in range(N):
dp[i][i] = piles[i]
for d in range(1, N):
for i in range(N - d):
dp[i][i + d] = max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[None for _ in range(n)] for _ in range(n)]
for l in range(1, n):
for i in range(n - l):
j = i + l
if j == i + 1:
dp[i][j] = abs(piles[i] - piles[j])
else:
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = [[0] * len(piles) for _ in range(len(piles))]
for i in range(len(piles)):
dp[i][i] = piles[i]
for i in range(len(piles) - 1)[::-1]:
for j in range(i + 1, len(piles)):
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for interval in range(1,n):
for left in range(n-interval):
dp[left][left+interval] = max(piles[left]-dp[left+1][left+interval],piles[left+interval] - dp[left][left+interval-1])
return dp[0][-1]>0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n=len(piles)
dp=[[0 for i in range(n)] for j in range(n)]
for i in range(n):
dp[i][i]=piles[i]
for m in range(2,n+1):
for l in range(n-m+1):
r=l+m-1
dp[l][r]=max(piles[l]-dp[l+1][r], piles[r]-dp[l][r-1])
return dp[0][n-1]>0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
p = piles
n = len(p)
dp = [[0] * n for i in range(n)]
for i in range(n): dp[i][i] = p[i]
for d in range(1, n):
for i in range(n - d):
dp[i][i + d] = max(p[i] - dp[i + 1][i + d], p[i + d] - dp[i][i + d - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# [3,8,5,1]
return True
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# dp[i][j], total score the Alex can get with pile[i:j + 1]
n = len(piles)
dp = [[0 for i in range(n)] for j in range(n)]
for l in range(2, n + 1):
for i in range(n - l + 1):
j = i + l - 1
if l % 2 == 0: # Alex next move
dp[i][j] = max(dp[i + 1][j] + piles[i], dp[i][j - 1] + piles[j])
else:
dp[i][j] = min(dp[i + 1][j] - piles[i], dp[i][j - 1] - piles[j])
return dp[0][n - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
if not piles or len(piles) == 0:
return False
dp = [[0] * len(piles) for _ in range(len(piles))]
for i in range(len(piles)):
dp[i][i] = piles[i]
for length in range(2, len(piles) + 1):
for i in range(0, len(piles) - length + 1):
j = i + length - 1
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1])
return dp[0][len(piles) - 1]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
m = len(piles)
dp = [[0 for _ in range(m)] for _ in range(m)]
for i in range(m):
dp[i][i] = piles[i]
for length in range(2, m + 1):
for start in range(0, m, 1):
end = start + length - 1
if end >= m:
break
dp[start][end] = max(piles[start] - dp[start+1][end], piles[end] - dp[start][end-1])
return dp[0][m - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles):
grid = [[0]*len(piles) for x in range(len(piles))]
for i in range(len(piles)-1, -1, -1):
for j in range(len(piles)):
if i >= j: grid[i][j] = 0
else:
#alex
if (j-i)%2 == 1:
grid[i][j] = max(piles[i] + grid[i+1][j], piles[j] + grid[i][j-1])
#lee
else:
grid[i][j] = min(-piles[i] + grid[i+1][j], -piles[j] + grid[i][j-1])
return grid[0][len(piles)-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = [[(0,0)] * len(piles) for _ in range(len(piles))]
for i in range(len(piles)):
dp[i][i] = (piles[i], 0)
for diff in range(1,len(piles)):
for j in range(len(piles) - diff):
i = diff + j
left = dp[j+1][i][1] + piles[j]
right = dp[j][i-1][1] + piles[i]
if left > right:
dp[j][i] = (left, dp[j+1][i][0])
else:
dp[j][i] = (right, dp[j][i-1][0])
return dp[0][-1][0] > dp[0][-1][1]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def get_score(i, j):
if i > j:
return 0
if i == j:
return piles[i]
return max(piles[i] + get_score(i + 2, j), piles[i] + get_score(i + 1, j - 1),
piles[j] + get_score(i + 1, j - 1), piles[j] + get_score(i, j - 2))
return 2 * get_score(0, len(piles) - 1) > sum(piles)
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# use dp
# answer the question \"what is the expected gain of the current player over the next player given the plies[i : j] (inclusive)\"
table = [[0 for _ in range(len(piles))] for _ in range(len(piles))]
# base case - single element
for i in range(len(piles)):
table[i][i] = piles[i]
for diff in range(1, len(piles)):
for i in range(0, len(piles) - diff):
table[i][i + diff] = max(piles[i] - table[i + 1][i + diff],
piles[i + diff] - table[i][i + diff - 1])
return table[0][len(piles) - 1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, p: List[int]) -> bool:
n = len(p)
dp = [[0]*n for _ in range(n)]
for i in range(n):
dp[i][i] = p[i]
for d in range(1, n):
for i in range(n - d):
# print(i, i+d)
dp[i][i+d] = max(p[i]-dp[i+1][i+d], p[i+d]-dp[i][i+d-1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles):
n = len(piles)
dp = [[0 for _ in range(n)] for _ in range(n)]
presum = [0]
for p in piles:
presum.append(p + presum[-1])
for i in range(n):
dp[i][i] = piles[i]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
dp[i][j] = max(piles[i] + presum[j + 1] - presum[i + 1] - dp[i + 1][j],
piles[j] + presum[j] - presum[i] - dp[i][j - 1])
return dp[0][n - 1] > presum[-1] - dp[0][n - 1]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for i in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for d in range(1, n + 1):
for i in range(n - d):
dp[i][i + d] = max(piles[i] - dp[i + 1][i + d], piles[i + d] - dp[i][i + d - 1])
return dp[0][-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0] * n for _ in range(n)]
for i in range(n): dp[i][i] = piles[i]
cum = [0] * n
cum[0] = piles[0]
for i in range(1, n):
cum[i] = cum[i-1] + piles[i]
for l in range(1, n):
for i in range(n - l):
j = i + l
dp[i][j] = max(piles[i] + cum[n-1] - dp[i+1][j], piles[j] + cum[n-1] - dp[i][j-1])
return dp[0][n - 1] > (cum[n-1] // 2)
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# dp[i][j] represents the max difference in score current player gets over other player
dp = [[0 for _ in range(len(piles))] for _ in range(len(piles))]
# base case where length of list is 1
for i in range(len(piles)):
dp[i][i] = piles[i]
start = len(piles) - 1
while start >= 0:
end = start + 1
while end < len(piles):
dp[start][end] = max(piles[start] - dp[start + 1][end], piles[end] - dp[start][end - 1])
end += 1
start -= 1
return dp[0][len(piles) - 1] >= 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)
dp = [[0]*n for _ in range(n)]
for i in range(n):
dp[i][i] = piles[i]
for i in range(n-1):
for j in range(n):
dp[i][j] = max(piles[i]-dp[i+1][j], piles[j] - dp[i][j - 1])
return dp[0][n-1] > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def game(left, right):
if left == right:
return piles[left]
return max(piles[left] - game(left + 1, right), piles[right] - game(left, right - 1))
return game(0, len(piles) - 1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
# time complexity: O(n^2)
# space complexity: O(n)
@lru_cache(maxsize=None)
def score(l, r):
if l == r: return piles[l]
return max(piles[l] - score(l+1, r),
piles[r] - score(l, r-1))
return score(0, len(piles)-1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(maxsize=None)
def score(l, r):
if l == r: return piles[l]
return max(piles[l] - score(l+1, r),
piles[r] - score(l, r-1))
return score(0, len(piles)-1) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memo = {}
return self.dfs(0, 0, piles, 0, len(piles)-1, memo)
def dfs(self, person1, person2, piles, start, end, memo):
if (person1, person2, start, end) in memo:
return memo[(person1, person2, start, end)]
if end-start == 1:
if person1 + max(piles) > person2 + min(piles):
return True
else:
return False
case1_person1 = person1 + piles[0]
res1 = self.dfs(person2, case1_person1, piles, start+1, end, memo)
if res1 == False:
memo[(person1, person2, start, end)] = True
return True
case2_person1 = person1+piles[-1]
res2 = self.dfs(person2, case2_person1, piles, start, end-1, memo)
if res2 == False:
memo[(person1, person2, start, end)] = True
return True
memo[(person1, person2, start, end)] = False
return False
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
# @lru_cache(None)
def stoneGame(self, piles: List[int]) -> bool:
result = dict()
@lru_cache(None)
def Alex(start, end) -> int:
if start == end:
return piles[start]
# if (start, end) in result:
# return result[(start, end)]
else:
# result[(start, end)] = max(Lee(start + 1, end) + piles[start], Lee(start, end - 1) + piles[end])
return max(Lee(start + 1, end) + piles[start], Lee(start, end - 1) + piles[end])
# return result[(start, end)]
def Lee(start, end) -> int:
if start == end:
return 0
# if (start, end) in result:
# return result[(start, end)]
else:
# result[(start, end)] = min(Alex(start + 1, end), Alex(start, end - 1))
return min(Alex(start + 1, end), Alex(start, end - 1))
# return result[(start, end)]
Alex(0, len(piles) - 1)
summ = sum(piles)
# print(result[(0, len(piles - 1))])
# print(summ)
return Alex(0, len(piles) - 1) > (summ - Alex(0, len(piles) - 1))
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
memory = [[0 for _ in range(len(piles))]for _ in range(len(piles))]
return self.dfs(piles, 0, len(piles)-1, True, memory) > 0
def dfs(self, piles, start, end, turn, memory):
if start > end:
return 0
if memory[start][end] != 0:
return memory[start][end]
if turn:
memory[start][end] = max(piles[start] + self.dfs(piles, start+1, end, not turn, memory), piles[end] + self.dfs(piles, start, end-1, not turn, memory))
else:
memory[start][end] = min(-piles[start] + self.dfs(piles, start+1, end, not turn, memory), -piles[end] + self.dfs(piles, start, end-1, not turn, memory))
return memory[start][end]
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
from functools import lru_cache
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
@lru_cache(None)
def helper(l,r):
if r-l == 1:
return piles[l]
if r-l == 2:
return abs(piles[l] - piles[l+1])
return max(piles[l] - helper(l+1, r), piles[r-1] - helper(l,r-1))
return helper(0,len(piles)) > 0
|
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones is odd, so there are no ties.
Alex and Lee take turns, with Alex starting first. Each turn, a player takes the entire pile of stones from either the beginning or the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alex and Lee play optimally, return True if and only if Alex wins the game.
Example 1:
Input: piles = [5,3,4,5]
Output: true
Explanation:
Alex starts first, and can only take the first 5 or the last 5.
Say he takes the first 5, so that the row becomes [3, 4, 5].
If Lee takes 3, then the board is [4, 5], and Alex takes 5 to win with 10 points.
If Lee takes the last 5, then the board is [3, 4], and Alex takes 4 to win with 9 points.
This demonstrated that taking the first 5 was a winning move for Alex, so we return true.
Constraints:
2 <= piles.length <= 500
piles.length is even.
1 <= piles[i] <= 500
sum(piles) is odd.
|
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n=len(piles)
if n//2==0:
return True
dp=[[0 for i in range(n)] for j in range(n)]
prefix=[0]
for i in range(n):
dp[i][i]=piles[i]
prefix.append(prefix[-1]+piles[i])
for k in range(1,n):
for i in range(n):
j=i+k
if j>=n:
break
dp[i][j]=piles[j]+((prefix[j]-prefix[i])-dp[i][j-1])
dp[i][j]=max(dp[i][j],piles[i]+((prefix[j+1]-prefix[i+1])-dp[i+1][j]))
return (dp[0][-1]>sum(piles)//2)
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
# remember how many empty slots we have
# non-null nodes occupy one slot but create two new slots
# null nodes occupy one slot
p = preorder.split(',')
#initially we have one empty slot to put the root in it
slot = 1
for node in p:
# no empty slot to put the current node
if slot == 0:
return False
# a null node?
if node == '#':
# ocuppy slot
slot -= 1
else:
# create new slot
slot += 1
#we don't allow empty slots at the end
return slot==0
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
if not preorder:
return False
nodes = preorder.split(',')
stack = [0] if nodes[0] != '#' else []
dt = {0:2}
i = 1
while stack and i < len(nodes):
dt[stack[-1]] -= 1
if dt[stack[-1]] == 0:
stack.pop()
if nodes[i] != '#':
stack.append(i)
dt[i] = 2
i = i + 1
return not stack and i == len(nodes)
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
new_preorder = preorder
for i in range (len(new_preorder)-1,0,-1):
if (new_preorder[i] != '#' and new_preorder[i] != ',') and (new_preorder[i-1] != '#' and new_preorder[i-1] != ','):
preorder = preorder[:i] + preorder[i+1:]
#print(i)
print(preorder)
#print(int(((len(preorder)/2)+1)/2))
num = 0
sharp = 0
for i in range (0 , len(preorder)):
print((num,sharp))
if sharp > num:
return False
elif preorder[i] == '#':
#print(num,sharp)
sharp += 1
if sharp == num+1 and num == int(((len(preorder)/2)+1)/2):
return True
elif preorder[i] != ',':
num += 1
print((num,sharp))
if num != sharp - 1:
return False
"""
:type preorder: str
:rtype: bool
"""
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
arr = preorder.split(',')
k = len(arr)
for _ in range(k):
print(arr)
if arr==['#']:
return True
newArr = []
i = 0
while i < len(arr):
if i < len(arr)-2 and arr[i].isdigit() and arr[i+1:i+3] == ['#','#']:
newArr += ['#']
i += 3
else:
newArr += [arr[i]]
i += 1
arr = newArr
return False
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
preorder = preorder.split(',')
if preorder[0] == '#':
return len(preorder) == 1
s = []
curr = preorder[0]
on_left = True
for i in range(1, len(preorder)):
if not curr:
return False
e = preorder[i]
if e != '#':
if on_left:
s.append(curr)
curr = e
on_left = True
else:
if not on_left:
curr = s.pop() if len(s) > 0 else None
on_left = False
return curr is None
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
# '' valid or not?
if len(preorder) < 1:
return False
stack = []
for s in preorder.split(','):
stack.append(False)
if s == '#':
# remove pairing left branch
while len(stack) > 2 and stack[-2]:
stack.pop()
stack.pop()
stack.pop()
else:
stack.append(True)
return stack == [False, True]
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
if not preorder:
return True
arr = preorder.split(',')
s = []
for a in arr:
s.append(a)
while len(s)>=3 and s[-1]=='#' and s[-2]=='#' and s[-3]!='#':
s.pop()
s.pop()
s.pop()
s.append('#')
if s==['#']:
return True
return False
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
preorder = preorder.split(',')
if preorder[0] == '#':
return len(preorder) == 1
s = []
curr = preorder[0]
on_left = True
finish = False
for i in range(1, len(preorder)):
if not curr:
return False
e = preorder[i]
if e != '#':
s.append((curr, on_left))
curr = e
on_left = True
finish = False
else:
if not on_left:
curr = None
finish = True
while len(s) and not on_left:
curr, on_left = s.pop()
if curr and not on_left:
curr = None
on_left = False
return finish
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
stack,preorder = [],preorder.split(',')
top = -1
for s in preorder:
stack.append(s)
top += 1
while(self.endsWithTwoHashes(stack,top)):
h,top = stack.pop(),top-1
h,top = stack.pop(),top-1
if top < 0: return False
stack[-1] = '#'
#h = stack.pop()
#stack.append('#')
return len(stack) == 1 and stack[0]=='#'
def endsWithTwoHashes(self,stack,top):
if top<1:
return False
if stack[top]=='#' and stack[top-1]=='#':
return True
return False
|
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.
_9_
/ \
3 2
/ \ / \
4 1 # 6
/ \ / \ / \
# # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character '#' representing null pointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".
Example 1:
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:
Input: "1,#"
Output: false
Example 3:
Input: "9,#,#,1"
Output: false
|
class Solution:
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
preorder, first = preorder.split(","), preorder.split(",")
def backward(index):
if index >= len(preorder) or index < 0: return
if index+1<len(preorder) and preorder[index+1] == preorder[index] == "#" and index-1 >= 0 and preorder[index-1] != "#":
preorder.pop(index)
preorder.pop(index)
preorder[index-1] = "#"
backward(index-2)
else: backward(index+1)
backward(0)
return True if (preorder != first and preorder == ["#"]) or (preorder == first == ["#"]) else False
|
Given two strings text1 and text2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
The input strings consist of lowercase English characters only.
|
class Solution:
def longestCommonSubsequence(self, a: str, b: str) -> int:
last, current = [0] * (len(b) + 1), [0] * (len(b) + 1)
for i in range(len(a) - 1, -1, -1):
for j in range(len(b) - 1, -1, -1):
if a[i] == b[j]:
current[j] = 1 + last[j + 1]
else:
current[j] = max(last[j], current[j + 1])
last = current
current = [0] * (len(b) + 1)
return last[0]
|
Given two strings text1 and text2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
The input strings consist of lowercase English characters only.
|
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
n, m = len(text1), len(text2)
if n == 0 or m == 0:
return max(n, m)
DeleteT1 = 0
DeleteT2 = 1
Match = 2
dp = [[(0, None) for j in range(m+1)] for i in range(n+1)] ###(value, direction)
for i in range(1,n+1):
dp[i][0] = (i,DeleteT1)
for j in range(1,m+1):
dp[0][j] = (j,DeleteT2)
for i in range(1, n+1):
for j in range(1, m+1):
if text1[i-1] == text2[j-1]:
dp[i][j] = (dp[i-1][j-1][0],Match)
continue
val = min(dp[i-1][j][0], dp[i][j-1][0])
if val == dp[i-1][j][0]: ###not dp[i-1][j]
dp[i][j] = (val+1,DeleteT1)
else:
dp[i][j] = (val+1,DeleteT2)
#print(len(dp), len(dp[0]))
#print(dp)
i,j = n,m
T1Length = n
while i >= 0 and j >= 0:
comb = dp[i][j]
if comb[1] == DeleteT1:
i -= 1
T1Length -= 1
elif comb[1] == DeleteT2:
j -= 1
else:
i -= 1
j -= 1
#print(dp)
return T1Length
|
Given two strings text1 and text2, return the length of their longest common subsequence.
A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order of the remaining characters. (eg, "ace" is a subsequence of "abcde" while "aec" is not). A common subsequence of two strings is a subsequence that is common to both strings.
If there is no common subsequence, return 0.
Example 1:
Input: text1 = "abcde", text2 = "ace"
Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.
Example 2:
Input: text1 = "abc", text2 = "abc"
Output: 3
Explanation: The longest common subsequence is "abc" and its length is 3.
Example 3:
Input: text1 = "abc", text2 = "def"
Output: 0
Explanation: There is no such common subsequence, so the result is 0.
Constraints:
1 <= text1.length <= 1000
1 <= text2.length <= 1000
The input strings consist of lowercase English characters only.
|
class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
s1,s2=text1,text2
len_s1,len_s2=len(s1),len(s2)
pick_s1_or_s2,pick_s1,pick_s2=0,1,2
match_table=[[0 for i in range(len_s2)] for j in range(len_s1)]
for i in range(len_s1):
for j in range(len_s2):
if(s1[i]==s2[j]):
if(i==0 or j==0):
match_table[i][j]=1
else:
match_table[i][j]=1+match_table[i-1][j-1]
else:
if(i==0 and j>0):
match_table[i][j]=match_table[i][j-1]
elif(j==0 and i>0):
match_table[i][j]=match_table[i-1][j]
elif(i>0 and j>0):
if(match_table[i-1][j]>match_table[i][j-1]):
match_table[i][j]=match_table[i-1][j]
else:
match_table[i][j]=match_table[i][j-1]
return match_table[len_s1-1][len_s2-1]
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.