description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones: List[int]) -> str:
rowlen = len(stones)
i = rowlen - 1
i_1, i_2, i_3 = 0, 0, 0
while i >= 0:
answer = -float("inf")
answer = max(answer, stones[i] - i_1)
if i + 1 < rowlen:
answer = max(answer, stones[i] + stones[i + 1] - i_2)
if i + 2 < rowlen:
answer = max(answer, stones[i] + stones[i + 1] + stones[i + 2] - i_3)
i_3 = i_2
i_2 = i_1
i_1 = answer
i -= 1
if i_1 > 0:
return "Alice"
elif i_1 < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
length = len(stoneValue)
dp = [(0) for _ in range(length + 1)]
for i in range(length - 1, -1, -1):
take = 0
dp[i] = -sys.maxsize
for j in range(3):
if i + j < length:
take += stoneValue[i + j]
dp[i] = max(dp[i], take - dp[i + j + 1])
if dp[0] > 0:
return "Alice"
elif dp[0] < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, s: List[int]) -> str:
cost = [-1] * len(s)
def helper(s, i):
if i >= len(s):
return 0
if cost[i] != -1:
return cost[i]
ans = -sys.maxsize
ans = max(ans, s[i] - helper(s, i + 1))
if i + 1 < len(s):
ans = max(ans, s[i] + s[i + 1] - helper(s, i + 2))
if i + 2 < len(s):
ans = max(ans, s[i] + s[i + 1] + s[i + 2] - helper(s, i + 3))
cost[i] = ans
return cost[i]
a = helper(s, 0)
if a > 0:
return "Alice"
if a == 0:
return "Tie"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
length = len(stoneValue)
@lru_cache(maxsize=None)
def helper(current_idx):
if current_idx >= length:
return 0
res = float("-inf")
sum = 0
for i in range(3):
if current_idx + i < length:
sum += stoneValue[current_idx + i]
res = max(res, sum - helper(current_idx + i + 1))
return res
result = helper(0)
if result == 0:
return "Tie"
elif result > 0:
return "Alice"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: list) -> str:
def play(preSum, index, cache):
if index == len(preSum) - 1:
return preSum[index]
if index in cache:
return cache[index]
res = -(2**31)
for i in range(1, 4):
if index + i >= len(preSum):
break
take = preSum[index] - preSum[index + i]
res = max(
res, take + preSum[index + i] - play(preSum, index + i, cache)
)
cache[index] = res
return res
for i in range(len(stoneValue) - 2, -1, -1):
stoneValue[i] += stoneValue[i + 1]
aliceScore = play(stoneValue + [0], 0, {})
bobScore = stoneValue[0] - aliceScore
if bobScore > aliceScore:
return "Bob"
elif aliceScore > bobScore:
return "Alice"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR LIST NUMBER NUMBER DICT ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
arr = stoneValue[:]
dp = [(0) for _ in range(3)]
for i in range(len(arr) - 1, -1, -1):
dp[i % 3] = max(sum(arr[i : i + k]) - dp[(i + k) % 3] for k in range(1, 4))
return "Alice" if dp[0] > 0 else "Bob" if dp[0] < 0 else "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
prefix = list(itertools.accumulate(stoneValue))
@lru_cache(maxsize=None)
def getScore(i):
if i > len(stoneValue):
return 0
stones = float("-inf")
for j in range(1, 3 + 1):
prev = prefix[i - 1] if i - 1 >= 0 else 0
stones = max(stones, prefix[-1] - prev - getScore(i + j))
return stones
alice_score = getScore(0)
if alice_score > prefix[-1] - alice_score:
return "Alice"
elif alice_score < prefix[-1] - alice_score:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR RETURN STRING IF VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
dp = [0] * (n + 3)
total = 0
for i in range(n - 1, -1, -1):
total += stoneValue[i]
dp[i] = -float("inf")
for k in range(1, 4, 1):
dp[i] = max(total - dp[i + k], dp[i])
oppo = total - dp[0]
if dp[0] > oppo:
return "Alice"
elif dp[0] < oppo:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR RETURN STRING IF VAR NUMBER VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
memo = {}
post_sum = [0] * (n + 1)
for i in range(n)[::-1]:
post_sum[i] = stoneValue[i] + post_sum[i + 1]
def dfs(i):
if i >= n:
return 0
if i in memo:
return memo[i]
res = float("-inf")
cur = 0
for j in range(i, min(i + 3, n)):
cur += stoneValue[j]
res = max(res, cur + post_sum[j + 1] - dfs(j + 1))
memo[i] = res
return res
res = dfs(0)
if res * 2 == post_sum[0]:
return "Tie"
if res * 2 > post_sum[0]:
return "Alice"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
self.cache = [0] * 50001
total = sum(stoneValue)
first = (total + self.helper(stoneValue, 0)) // 2
second = total - first
if first > second:
return "Alice"
elif first < second:
return "Bob"
else:
return "Tie"
def helper(self, stoneValue, index):
if self.cache[index]:
return self.cache[index]
n = len(stoneValue)
if index == n:
return 0
cur = 0
best = -sys.maxsize
for i in range(3):
if index + i >= n:
break
cur += stoneValue[index + i]
best = max(best, cur - self.helper(stoneValue, index + i + 1))
self.cache[index] = best
return best | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
firstSelect = True
initIndex = 0
stoneValueLength = len(stoneValue)
mem = {}
if stoneValueLength == 0:
return "Tie"
else:
result = self.recursiveHelper(
stoneValue, stoneValueLength, initIndex, firstSelect, mem
)
if result > 0:
return "Alice"
elif result < 0:
return "Bob"
else:
return "Tie"
def recursiveHelper(
self, stoneValue: List[int], length: int, index: int, flag: bool, mem: dict
) -> int:
if index >= length:
return 0
memV = mem.get((index, flag))
if memV is not None:
return memV
minus = length - index
if minus == 1:
curr = stoneValue[index]
c1 = curr if flag else -curr
mem[index, flag] = c1
return c1
elif minus == 2:
curr = stoneValue[index] + stoneValue[index + 1]
c1 = curr if flag else -curr
curr = stoneValue[index]
c2 = (curr if flag else -curr) + self.recursiveHelper(
stoneValue, length, index + 1, not flag, mem
)
result = max(c1, c2) if flag else min(c1, c2)
mem[index, flag] = result
return result
else:
curr = stoneValue[index] + stoneValue[index + 1] + stoneValue[index + 2]
c1 = (curr if flag else -curr) + self.recursiveHelper(
stoneValue, length, index + 3, not flag, mem
)
curr = stoneValue[index] + stoneValue[index + 1]
c2 = (curr if flag else -curr) + self.recursiveHelper(
stoneValue, length, index + 2, not flag, mem
)
curr = stoneValue[index]
c3 = (curr if flag else -curr) + self.recursiveHelper(
stoneValue, length, index + 1, not flag, mem
)
result = max(c1, c2, c3) if flag else min(c1, c2, c3)
mem[index, flag] = result
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT IF VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR FUNC_DEF VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
memo = dict()
def dfs(i):
if i >= N:
return 0
if i in memo:
return memo[i]
memo[i] = suffixsum[i] - min([dfs(i + x) for x in range(1, 4)])
return memo[i]
N = len(stoneValue)
suffixsum, acc = [0] * N, 0
for i, v in enumerate(reversed(stoneValue)):
acc += v
suffixsum[~i] = acc
alice = dfs(0)
bob = suffixsum[0] - alice
if alice > bob:
return "Alice"
elif alice == bob:
return "Tie"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
memo = {}
def dfs(stoneValue, i, memo):
if i in memo:
return memo[i]
if i >= len(stoneValue):
return 0
if i == len(stoneValue) - 1:
return stoneValue[i]
maxi = stoneValue[i] + min(
dfs(stoneValue, i + 2, memo),
dfs(stoneValue, i + 3, memo),
dfs(stoneValue, i + 4, memo),
)
if i + 1 < len(stoneValue):
maxi = max(
maxi,
stoneValue[i]
+ stoneValue[i + 1]
+ min(
dfs(stoneValue, i + 3, memo),
dfs(stoneValue, i + 4, memo),
dfs(stoneValue, i + 5, memo),
),
)
if i + 2 < len(stoneValue):
maxi = max(
maxi,
sum(stoneValue[i : i + 3])
+ min(
dfs(stoneValue, i + 4, memo),
dfs(stoneValue, i + 5, memo),
dfs(stoneValue, i + 6, memo),
),
)
memo[i] = maxi
return maxi
aliceScore = dfs(stoneValue, 0, memo)
bobScore = sum(stoneValue) - aliceScore
if aliceScore > bobScore:
return "Alice"
elif aliceScore < bobScore:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones: List[int]) -> str:
rowlen = len(stones)
dp = [0] * (rowlen + 1)
i = rowlen - 1
while i >= 0:
answer = -float("inf")
answer = max(answer, stones[i] - dp[i + 1])
if i + 1 < rowlen:
answer = max(answer, stones[i] + stones[i + 1] - dp[i + 2])
if i + 2 < rowlen:
answer = max(
answer, stones[i] + stones[i + 1] + stones[i + 2] - dp[i + 3]
)
dp[i] = answer
i -= 1
print(dp)
if dp[0] > 0:
return "Alice"
elif dp[0] < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
dp = [-float("inf")] * (len(stoneValue) + 5)
dp[len(stoneValue) : len(stoneValue) + 5] = [0] * 5
s = stoneValue
dp = dict()
def search(i):
if i >= len(stoneValue):
return 0
res = -float("inf")
score = 0
if i in dp:
return dp[i]
for j in range(3):
if i + j < len(stoneValue):
score += stoneValue[i + j]
res = max(score - search(i + j + 1), res)
dp[i] = res
return dp[i]
ans = search(0)
if dp[0] == 0:
return "Tie"
return "Alice" if dp[0] > 0 else "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING RETURN VAR NUMBER NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
memo = {}
def dfs(i, bob):
if (i, bob) in memo:
return memo[i, bob]
if i >= n:
return 0
ans = float("inf") if bob else float("-inf")
for x in [1, 2, 3]:
sub = dfs(i + x, 1 - bob)
if bob:
ans = min(ans, sub)
else:
ans = max(ans, sub + sum(stoneValue[i : i + x]))
memo[i, bob] = ans
return ans
dfs(0, 0)
dfs(0, 1)
if memo[0, 1] == memo[0, 0]:
return "Tie"
return "Bob" if memo[0, 1] > memo[0, 0] else "Alice" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN STRING RETURN VAR NUMBER NUMBER VAR NUMBER NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
N = len(stoneValue)
dp = [0] * (N + 3)
acc = 0
for i in range(N - 1, -1, -1):
acc += stoneValue[i]
dp[i] = acc - min(dp[i + x] for x in range(1, 4))
alice = dp[0]
bob = acc - alice
if alice > bob:
return "Alice"
elif alice == bob:
return "Tie"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
record = {}
def helper(i):
if i in record:
return record[i]
if i >= len(stoneValue):
return [0, 0]
a, b = float("-inf"), float("-inf")
for x in range(i, min(i + 3, len(stoneValue))):
temp = helper(x + 1)
tempa, tempb = sum(stoneValue[i : x + 1]) + temp[1], temp[0]
if tempa > a:
a, b = tempa, tempb
record[i] = [a, b]
return [a, b]
a, b = helper(0)
if a > b:
return "Alice"
elif a < b:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR RETURN LIST VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
presum = [0]
for num in stoneValue:
presum.append(presum[-1] + num)
@lru_cache(None)
def dfs(i):
if i >= len(stoneValue):
return 0
ans = float("-inf")
temp = 0
for k in range(3):
if i + k < len(stoneValue):
temp += stoneValue[i + k]
remain = presum[-1] - presum[i + k + 1]
ans = max(ans, temp + remain - dfs(i + k + 1))
return ans
total = dfs(0)
if total * 2 > presum[-1]:
return "Alice"
elif total * 2 < presum[-1]:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
cursum = 0
dp1 = [float("-inf")] * (n + 1)
dp1[-1] = 0
for i in range(n - 1, -1, -1):
cursum += stoneValue[i]
for k in range(3):
if i + k >= n:
break
dp1[i] = max(dp1[i], cursum - dp1[i + k + 1])
if dp1[0] > cursum - dp1[0]:
return "Alice"
if dp1[0] < cursum - dp1[0]:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN STRING IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
if not stoneValue:
return "Tie"
pre_sum = [(0) for _ in range(len(stoneValue) + 1)]
for i in range(len(stoneValue)):
pre_sum[i + 1] = pre_sum[i] + stoneValue[i]
@lru_cache(None)
def dfs(idx, player):
if idx > len(stoneValue) - 1:
return 0
if player == 1:
res = float("-inf")
for i in range(1, 4):
if idx + i > len(stoneValue):
break
res = max(
res, pre_sum[idx + i] - pre_sum[idx] + dfs(idx + i, -player)
)
else:
res = float("inf")
for i in range(1, 4):
res = min(res, dfs(idx + i, -player))
return res
a_sum = dfs(0, 1)
b_sum = pre_sum[-1] - a_sum
if a_sum == b_sum:
return "Tie"
elif a_sum > b_sum:
return "Alice"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
dp = [float("-inf")] * n
def dfs(i):
if i >= n:
return 0
if dp[i] != float("-inf"):
return dp[i]
ssum = 0
for j in range(3):
if i + j < n:
ssum += stoneValue[i + j]
dp[i] = max(dp[i], ssum - dfs(i + j + 1))
return dp[i]
score = dfs(0)
if score > 0:
return "Alice"
elif score < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
dp = [float("-inf")] * len(stoneValue)
self.presum = [0] * (len(stoneValue) + 1)
for idx, num in enumerate(stoneValue[::-1]):
self.presum[len(stoneValue) - idx - 1] = (
self.presum[len(stoneValue) - idx] + num
)
self.presum.pop()
alice_score = self.helper(stoneValue, 0, dp)
if alice_score > sum(stoneValue) - alice_score:
return "Alice"
elif alice_score < sum(stoneValue) - alice_score:
return "Bob"
else:
return "Tie"
def helper(self, nums, start, dp):
if dp[start] != float("-inf"):
return dp[start]
if start == len(nums) - 1:
dp[start] = nums[start]
return nums[start]
if start == len(nums) - 2:
dp[start] = max(self.presum[start], nums[start])
return dp[start]
if start == len(nums) - 3:
dp[start] = max(
self.presum[start],
self.presum[start] - self.helper(nums, start + 1, dp),
self.presum[start] - self.helper(nums, start + 2, dp),
)
return dp[start]
num1 = self.presum[start] - self.helper(nums, start + 1, dp)
num2 = (
self.presum[start] - self.helper(nums, start + 2, dp)
if start + 2 < len(nums)
else float("-inf")
)
num3 = (
self.presum[start] - self.helper(nums, start + 3, dp)
if start + 3 < len(nums)
else float("-inf")
)
dp[start] = max(num1, num2, num3)
return dp[start] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN STRING IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR VAR FUNC_CALL VAR STRING RETURN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stns: List[int]) -> str:
stns.reverse()
if len(stns) > 1:
ans = [0, stns[0], stns[1] + abs(stns[0])]
for i in range(2, len(stns)):
ans.append(
max(
stns[i] + stns[i - 1] + stns[i - 2] - ans[-3],
stns[i] + stns[i - 1] - ans[-2],
stns[i] - ans[-1],
)
)
else:
ans = stns
return "Alice" if ans[-1] > 0 else "Tie" if ans[-1] == 0 else "Bob" | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
prefix = [0] * (len(stoneValue) + 1)
for i in range(len(stoneValue)):
prefix[i + 1] = prefix[i] + stoneValue[i]
memo = {}
aliceScore = self.dfs(stoneValue, 0, memo, prefix)
total = sum(stoneValue)
if total - aliceScore > aliceScore:
return "Bob"
elif total - aliceScore < aliceScore:
return "Alice"
return "Tie"
def dfs(self, stoneValue, i, memo, prefix):
if i == len(stoneValue):
return 0
if i in memo:
return memo[i]
ans = (
stoneValue[i]
+ prefix[-1]
- prefix[i + 1]
- self.dfs(stoneValue, i + 1, memo, prefix)
)
if i + 1 < len(stoneValue):
ans = max(
ans,
stoneValue[i]
+ stoneValue[i + 1]
+ prefix[-1]
- prefix[i + 2]
- self.dfs(stoneValue, i + 2, memo, prefix),
)
if i + 2 < len(stoneValue):
ans = max(
ans,
stoneValue[i]
+ stoneValue[i + 1]
+ stoneValue[i + 2]
+ prefix[-1]
- prefix[i + 3]
- self.dfs(stoneValue, i + 3, memo, prefix),
)
memo[i] = ans
return memo[i] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN STRING IF BIN_OP VAR VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
memo = {}
def helper(i, total):
if i >= n:
return 0
elif i in memo:
return memo[i]
res = -math.inf
for x in range(1, min(4, n - i + 1)):
s = sum(stoneValue[i : i + x])
res = max(res, total - helper(i + x, total - s))
memo[i] = res
return res
total = sum(stoneValue)
a = helper(0, total)
b = total - a
if a == b:
return "Tie"
elif a > b:
return "Alice"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
first, second = self.memo_search(0, stoneValue, {})
if first > second:
return "Alice"
if first == second:
return "Tie"
return "Bob"
def memo_search(self, pos, stoneValue, memo):
if pos in memo:
return memo[pos]
if pos == len(stoneValue):
return 0, 0
ans = -sys.maxsize
value = 0
for i in range(3):
if pos + i < len(stoneValue):
value += stoneValue[pos + i]
second, first = self.memo_search(pos + i + 1, stoneValue, memo)
total = first + second + value
if first + value > ans:
ans = first + value
memo[pos] = ans, total - ans
return ans, total - ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR DICT IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue):
def dfs(start):
if start >= len(stoneValue):
return 0
if dp[start] != None:
return dp[start]
i = start
curSum = 0
maxValue = -float("inf")
while i < len(stoneValue) and i < start + 3:
curSum += stoneValue[i]
maxValue = max(maxValue, curSum - dfs(i + 1))
i += 1
dp[start] = maxValue
return dp[start]
dp = [None] * len(stoneValue)
res = dfs(0)
if res > 0:
return "Alice"
elif res < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones: List[int]) -> str:
n = len(stones)
dp = [(-math.inf) for _ in range(n + 1)]
dp[n] = 0
prefix = [(0) for _ in range(n + 1)]
for i in range(1, n + 1):
prefix[i] = prefix[i - 1] + stones[i - 1]
for i in range(n - 1, -1, -1):
for k in range(1, 4):
if i + k > n:
break
dp[i] = max(
dp[i],
sum(stones[i : i + k]) + prefix[-1] - prefix[i + k] - dp[i + k],
)
if dp[0] * 2 > sum(stones):
return "Alice"
elif dp[0] * 2 < sum(stones):
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR RETURN STRING IF BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, S: List[int]) -> str:
n = len(S)
dp = [0] * n
for i in range(n)[::-1]:
M = float("-inf")
for k in range(1, 4):
M = max(M, sum(S[i : i + k]) - (dp[i + k] if i + k < n else 0))
dp[i] = M
if dp[0] > 0:
return "Alice"
elif dp[0] < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
N = len(stoneValue)
F = [0] * (N + 1)
S = [0] * (N + 1)
for i in range(N - 1, -1, -1):
max_val = -100000000
for x in range(min(3, N - i)):
val = sum(stoneValue[i : i + x + 1]) + S[i + x + 1]
if max_val < val:
max_val = val
F[i] = val
S[i] = F[i + x + 1]
if F[0] == S[0]:
return "Tie"
return "Alice" if F[0] > S[0] else "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING RETURN VAR NUMBER VAR NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
self.memo = collections.defaultdict(int)
self.stoneValue = stoneValue
score = self.dfs(0)
return "Tie" if score == 0 else "Alice" if score > 0 else "Bob"
def dfs(self, start):
if start >= len(self.stoneValue):
return 0
if start in self.memo:
return self.memo[start]
res = float("-inf")
score = 0
for i in range(start, min(len(self.stoneValue), start + 3)):
score += self.stoneValue[i]
res = max(res, score - self.dfs(i + 1))
self.memo[start] = res
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
p1, p2, p3 = 0, 0, 0
post_sum = 0
for i in range(len(stoneValue) - 1, -1, -1):
val = stoneValue[i]
post_sum += val
best = post_sum - min([p1, p2, p3])
p1, p2, p3 = best, p1, p2
if p1 > post_sum - p1:
return "Alice"
elif p1 < post_sum - p1:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR RETURN STRING IF VAR BIN_OP VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
@lru_cache(maxsize=None)
def solve(i: int) -> int:
if i >= len(stoneValue):
return 0
res = -math.inf
for stones in range(1, 4):
curr_val = sum(stoneValue[i : i + stones]) + min(
solve(i + stones + 1), solve(i + stones + 2), solve(i + stones + 3)
)
res = max(res, curr_val)
return res
alice = solve(0)
bob = sum(stoneValue) - alice
if alice == bob:
return "Tie"
elif alice > bob:
return "Alice"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
@lru_cache(None)
def dp(cur):
if cur == len(stoneValue) - 1:
return stoneValue[cur]
if cur >= len(stoneValue):
return 0
ret = float("-inf")
s = 0
for i in range(3):
nxt = dp(cur + i + 1)
s += stoneValue[cur + i] if cur + i < len(stoneValue) else 0
ret = max(ret, s - nxt)
return ret
ans = dp(0)
if ans > 0:
return "Alice"
elif ans < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
p = [0] * n
for i in range(n - 1, -1, -1):
if i == n - 1:
p[i] = stoneValue[i]
else:
p[i] = stoneValue[i] + p[i + 1]
dp = [0] * (n + 1)
for i in range(n - 1, -1, -1):
dp[i] = p[i] - min(dp[i + 1 : i + 4])
if dp[0] * 2 == p[0]:
return "Tie"
elif dp[0] * 2 > p[0]:
return "Alice"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN STRING IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | from itertools import accumulate
class Solution:
def stoneGameIII(self, sv: List[int]) -> str:
m = len(sv)
dp = [-sys.maxsize] * m
dp.append(0)
ps = list(accumulate(sv))
sm = sum(sv)
for i in range(m - 1, -1, -1):
if i == m - 1:
dp[i] = sv[i]
elif i == m - 2:
dp[i] = max(dp[i], sv[i] + (ps[-1] - ps[i] - dp[i + 1]))
dp[i] = max(dp[i], sv[i] + sv[i + 1])
else:
dp[i] = max(dp[i], sv[i] + (ps[-1] - ps[i] - dp[i + 1]))
dp[i] = max(dp[i], sv[i] + sv[i + 1] + (ps[-1] - ps[i + 1] - dp[i + 2]))
dp[i] = max(
dp[i],
sv[i] + sv[i + 1] + sv[i + 2] + (ps[-1] - ps[i + 2] - dp[i + 3]),
)
if dp[0] * 2 == sm:
return "Tie"
if dp[0] * 2 > sm:
return "Alice"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR RETURN STRING IF BIN_OP VAR NUMBER NUMBER VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
@lru_cache(None)
def helper(idx):
if idx == len(stoneValue):
return 0, idx
score, max_score = 0, -sys.maxsize + 1
op_idx = idx + 1
for num in range(3):
if idx + num < len(stoneValue):
score += stoneValue[idx + num]
b_score, a_idx = helper(idx + num + 1)
if helper(a_idx)[0] + score >= max_score:
max_score = helper(a_idx)[0] + score
op_idx = idx + num + 1
return max_score, op_idx
score, op_idx = helper(0)
op_score = helper(op_idx)[0]
if score > op_score:
return "Alice"
elif score == op_score:
return "Tie"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NONE ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
remain = list(itertools.accumulate(stoneValue[::-1]))[::-1]
@lru_cache(None)
def dp(idx):
if idx >= len(stoneValue):
return 0
return remain[idx] - min(dp(idx + i) for i in range(1, 4))
first = dp(0)
second = remain[0] - first
return "Alice" if first > second else "Tie" if first == second else "Bob"
def stoneGameIII(self, stoneValue: List[int]) -> str:
remain = list(itertools.accumulate(stoneValue[::-1]))[::-1]
n = len(stoneValue)
dp = [0] * (n + 4)
for idx in range(n - 1, -1, -1):
dp[idx] = remain[idx] - min(dp[idx + i] for i in range(1, 4))
first = dp[0]
second = remain[0] - first
print([dp[i] for i in range(len(stoneValue))])
return "Alice" if first > second else "Tie" if first == second else "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR STRING VAR VAR STRING STRING VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR STRING VAR VAR STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
@lru_cache(None)
def dp(cur):
if cur >= n:
return 0, 0
first, second = -math.inf, -math.inf
for i in range(1, 4):
first_tmp, second_tmp = dp(cur + i)
if first < second_tmp + sum(stoneValue[cur : cur + i]):
first = second_tmp + sum(stoneValue[cur : cur + i])
second = first_tmp
return first, second
alice, bob = dp(0)
if alice > bob:
return "Alice"
elif alice < bob:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_CALL VAR NONE ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
if not stoneValue:
return "Tie"
n = len(stoneValue)
dp = [(0) for _ in range(n + 3)]
stoneValue = stoneValue + [0, 0, 0]
for i in range(n - 1, -1, -1):
max_v = float("-inf")
curr = stoneValue[i]
for j in range(i + 1, i + 4):
max_v = max(max_v, curr - dp[j])
curr += stoneValue[j]
dp[i] = max_v
if dp[0] > 0:
return "Alice"
if dp[0] < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
stoneN = len(stoneValue)
totalScore = sum(stoneValue)
dp = {}
def play(i0, ARound):
if i0 == stoneN:
return 0
if (i0, ARound) in dp:
return dp[i0, ARound]
if ARound:
subAns = float("-inf")
thisRoundScore = 0
for j in range(i0, min(i0 + 3, stoneN)):
thisRoundScore += stoneValue[j]
subAns = max(subAns, thisRoundScore + play(j + 1, False))
else:
subAns = float("inf")
for j in range(i0, min(i0 + 3, stoneN)):
subAns = min(subAns, play(j + 1, True))
dp[i0, ARound] = subAns
return subAns
aScore = play(0, True)
bScore = totalScore - aScore
if aScore == bScore:
return "Tie"
elif aScore > bScore:
return "Alice"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
total = sum(stoneValue)
dp = {}
alice_get = self.find_score(stoneValue, 0, dp)
if alice_get * 2 > total:
return "Alice"
elif alice_get * 2 < total:
return "Bob"
else:
return "Tie"
def find_score(self, stoneValue, i, dp):
if i in dp:
return dp[i]
if i == len(stoneValue) - 1:
return stoneValue[i]
elif i >= len(stoneValue):
return 0
elif i == len(stoneValue) - 2:
return max(stoneValue[i] + stoneValue[i + 1], stoneValue[i])
else:
dp[i] = max(
stoneValue[i]
+ min(
self.find_score(stoneValue, i + 2, dp),
self.find_score(stoneValue, i + 3, dp),
self.find_score(stoneValue, i + 4, dp),
),
stoneValue[i]
+ stoneValue[i + 1]
+ min(
self.find_score(stoneValue, i + 5, dp),
self.find_score(stoneValue, i + 3, dp),
self.find_score(stoneValue, i + 4, dp),
),
stoneValue[i]
+ stoneValue[i + 1]
+ stoneValue[i + 2]
+ min(
self.find_score(stoneValue, i + 5, dp),
self.find_score(stoneValue, i + 6, dp),
self.find_score(stoneValue, i + 4, dp),
),
)
return dp[i] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR RETURN STRING IF BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones):
total = sum(stones)
alice_value = self._dfs(stones, 0, {})
if alice_value > 0:
return "Alice"
elif alice_value < 0:
return "Bob"
return "Tie"
def _dfs(self, stones, start, memo):
if start >= len(stones):
return 0
if start in memo:
return memo[start]
res = -(2**31)
curr_total = 0
for i in range(start, min(start + 3, len(stones))):
curr_total += stones[i]
next_player_values = self._dfs(stones, i + 1, memo)
res = max(res, curr_total - next_player_values)
memo[start] = res
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER DICT IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
mem = {}
def helper(stoneValues, index):
if index in mem:
return mem[index]
if index >= len(stoneValues):
return 0
ans = stoneValues[index] - helper(stoneValues, index + 1)
if index + 1 < len(stoneValues):
temp = (
stoneValues[index]
+ stoneValues[index + 1]
- helper(stoneValues, index + 2)
)
ans = max(ans, temp)
if index + 2 < len(stoneValues):
temp = (
stoneValues[index]
+ stoneValues[index + 1]
+ stoneValues[index + 2]
- helper(stoneValues, index + 3)
)
ans = max(ans, temp)
mem[index] = ans
return ans
ans = helper(stoneValue, 0)
if ans > 0:
return "Alice"
elif ans < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
dp = [-(2**31)] * n
for i in range(n - 1, -1, -1):
take = 0
for j in range(i, min(i + 3, n)):
take += stoneValue[j]
dp[i] = max(dp[i], take - (dp[j + 1] if j + 1 < n else 0))
if dp[0] > 0:
return "Alice"
elif dp[0] < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
self.mem = dict()
self.n = len(stoneValue)
self.values = stoneValue
me, oppo = self.dfs(0)
if me > oppo:
return "Alice"
elif oppo > me:
return "Bob"
return "Tie"
def dfs(self, idx):
if idx >= self.n:
return 0, 0
if idx in self.mem:
return self.mem[idx]
oppo = 0
rv = -float("inf")
tmp = 0
for i in range(idx, min(self.n, idx + 3)):
rv1, rv2 = self.dfs(i + 1)
tmp += self.values[i]
if rv < tmp + rv2:
rv = tmp + rv2
oppo = rv1
self.mem[idx] = rv, oppo
return self.mem[idx] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF IF VAR VAR RETURN NUMBER NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones):
memo = dict()
n = len(stones)
def dp(s):
if s == n:
return 0
if s in memo:
return memo[s]
ans = -(2**31)
presum = 0
for i in range(1, 4):
if s + i - 1 >= n:
break
presum += stones[s + i - 1]
ans = max(ans, presum - dp(s + i))
memo[s] = ans
return ans
score = dp(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie"
def stoneGameIII5(self, stoneValue):
n = len(stoneValue)
memo = dict()
def dp(s):
if s >= n:
return 0
if s in memo:
return memo[s]
ans = -(2**31)
for i in range(1, 4):
if s + i > n:
break
ans = max(ans, sum(stoneValue[s : s + i]) - dp(s + i))
memo[s] = ans
return ans
score = dp(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie"
def stoneGameIII1(self, stoneValue):
n = len(stoneValue)
memo = dict()
def dp(s):
if s >= n:
return 0
if s in memo:
return memo[s]
ans = -(2**31)
for i in range(1, 4):
if s + i > n:
break
ans = max(ans, sum(stoneValue[s : s + i]) - dp(s + i))
memo[s] = ans
return ans
score = dp(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie"
class Solution1:
def stoneGameIII(self, stoneValue: List[int]) -> str:
memo = {}
n = len(stoneValue)
def dp(s):
if s >= n:
return 0
if s in memo:
return memo[s]
ans = -(2**31)
for i in range(1, 4):
if s + i > n:
break
ans = max(ans, sum(stoneValue[s : s + i]) - dp(s + i))
memo[s] = ans
return ans
score = dp(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie"
def stoneGameIII2(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
cache = dict()
def solve(s):
if s >= n:
return 0
if s in cache:
return cache[s]
best = -(2**31)
presum = 0
for x in range(1, 4):
if s + x - 1 >= n:
break
presum += stoneValue[s + x - 1]
best = max(best, presum - solve(s + x))
cache[s] = best
return best
score = solve(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie" | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
dp = {}
piles = stoneValue
n = len(piles)
sums = 0
for pile in piles:
sums += pile
def rec_max(i, n, sums):
if i == n:
return 0
if i in dp:
return dp[i][0]
m = min(3, n - i)
res_sum = float("-inf")
res_gap = float("-inf")
remains = sums
for x in range(m):
remains -= piles[i + x]
other = rec_max(i + x + 1, n, remains)
res = sums - other
gap = res - other
if gap > res_gap:
res_gap = gap
res_sum = res
dp[i] = res_sum, res_gap
return res_sum
rec_max(0, n, sums)
res = dp[0][1]
if res < 0:
return "Bob"
elif res > 0:
return "Alice"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
cache = dict()
def solve(s):
if s >= n:
return 0
if s in cache:
return cache[s]
best = -(2**31)
presum = 0
for x in range(1, 4):
if s + x - 1 >= n:
break
presum += stoneValue[s + x - 1]
best = max(best, presum - solve(s + x))
cache[s] = best
return best
score = solve(0)
return "Alice" if score > 0 else "Bob" if score < 0 else "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER STRING VAR NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
@lru_cache(None)
def dp(i, t):
if i == len(stoneValue):
return 0
if t == 0:
m = -math.inf
s = 0
for j in range(i, min(i + 3, len(stoneValue))):
s += stoneValue[j]
m = max(m, s + dp(j + 1, 1))
else:
m = math.inf
s = 0
for j in range(i, min(i + 3, len(stoneValue))):
s -= stoneValue[j]
m = min(m, s + dp(j + 1, 0))
return m
ans = dp(0, 0)
if ans > 0:
return "Alice"
elif ans < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue) -> str:
total_value = sum(stoneValue)
num_stones = len(stoneValue)
@lru_cache(None)
def helper(ind, player):
if ind >= num_stones:
return 0
if player == "A":
tmp_result = []
for i in range(1, 4):
if ind + i <= num_stones:
tmp_result.append(
sum(stoneValue[ind : ind + i]) + helper(ind + i, "L")
)
else:
tmp_result.append(
sum(stoneValue[ind:]) + helper(num_stones, "L")
)
return max(tmp_result)
elif player == "L":
tmp_result = []
for i in range(1, 4):
if ind + i <= num_stones:
tmp_result.append(helper(ind + i, "A"))
else:
tmp_result.append(helper(num_stones, "A"))
return min(tmp_result)
alex_score = helper(0, "A")
if alex_score > total_value / 2.0:
return "Alice"
elif alex_score < total_value / 2.0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN FUNC_CALL VAR VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER STRING IF VAR BIN_OP VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
@lru_cache(None)
def dp(i, chance):
n = len(stoneValue)
if i >= len(stoneValue):
return 0
if not chance:
ans = float("-inf")
for j in range(i, i + 3):
if j < n:
ans = max(ans, dp(j + 1, 1) + sum(stoneValue[i : j + 1]))
else:
ans = float("inf")
for j in range(i, i + 3):
if j < n:
ans = min(ans, dp(j + 1, 0) - sum(stoneValue[i : j + 1]))
return ans
score = dp(0, 0)
if score > 0:
return "Alice"
if score < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, piles: List[int]) -> str:
N = len(piles)
@lru_cache(None)
def sg(idx, turn):
if idx == N:
return 0
res = float("-inf")
if turn == -1:
res = float("inf")
ps = 0
for i in range(idx, min(idx + 3, N)):
ps += piles[i]
nxt = ps * turn + sg(i + 1, -turn)
if turn == 1:
res = max(res, nxt)
else:
res = min(res, nxt)
return res
res = sg(0, 1)
if res > 0:
return "Alice"
elif res < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
postsum, dp = 0, [0] * (len(stoneValue) + 3)
for i in range(len(stoneValue) - 1, -1, -1):
postsum += stoneValue[i]
dp[i] = postsum - min(dp[i + 1], dp[i + 2], dp[i + 3])
alice, bob = dp[0], postsum - dp[0]
if alice == bob:
return "Tie"
if alice > bob:
return "Alice"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
stoneValue.insert(0, 0)
dp = [-sys.maxsize] * (n + 1)
presum = [0] * (n + 1)
for i in range(1, n + 1):
presum[i] = presum[i - 1] + stoneValue[i]
dp[n] = 0
for i in reversed(range(n)):
temp = 0
for j in range(1, 4):
if i + j > n:
break
temp += stoneValue[i + j]
dp[i] = max(dp[i], temp + presum[n] - presum[i + j] - dp[i + j])
if dp[0] > presum[n] - dp[0]:
return "Alice"
elif dp[0] < presum[n] - dp[0]:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN STRING IF VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
memo = {}
n = len(stoneValue)
def solve(s):
if s >= n:
return 0
elif s in memo:
return memo[s]
currValue = 0
best = -(2**31)
for i in range(3):
if s + i >= n:
break
currValue += stoneValue[s + i]
best = max(best, currValue - solve(s + i + 1))
memo[s] = best
return best
ans = solve(0)
if ans > 0:
return "Alice"
elif ans < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
memo = {}
def dfs(start):
if start >= len(stoneValue):
return 0
if start in memo:
return memo[start]
memo[start] = -math.inf
score = 0
for i in range(start, min(len(stoneValue), start + 3)):
score += stoneValue[i]
memo[start] = max(memo[start], score - dfs(i + 1))
return memo[start]
score = dfs(0)
if score > 0:
return "Alice"
elif score == 0:
return "Tie"
else:
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
presum = [0] * (n + 1)
for i, num in enumerate(stoneValue):
presum[i + 1] = presum[i] + num
presum += [presum[-1]] * 3
dp = [([0] * 2) for _ in range(n + 3)]
for i in range(n - 1, -1, -1):
for k in range(2):
dp[i][k] = max(
-dp[j][1 - k] + presum[j] - presum[i] for j in range(i + 1, i + 4)
)
if dp[0][0] > 0:
return "Alice"
elif dp[0][0] < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP LIST VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
best = {}
S = 0
N = len(stoneValue)
cumsum = [(0) for val in stoneValue] + [0]
for ii in range(len(stoneValue) - 1, -1, -1):
S = S + stoneValue[ii]
cumsum[ii] = S
def findbest(index, stoneValue):
if index in best:
res = best[index]
elif index == len(stoneValue) - 1:
res = stoneValue[-1]
elif index >= len(stoneValue):
res = 0
else:
res = -10000000
for j in range(index + 1, min(index + 4, N + 1)):
res = max(
res,
sum(stoneValue[index:j]) + cumsum[j] - findbest(j, stoneValue),
)
best[index] = res
return best[index]
res = findbest(0, stoneValue)
if res > sum(stoneValue) / 2:
return "Alice"
elif res < sum(stoneValue) / 2:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones: List[int]) -> str:
rowlen = len(stones)
index = 0
memo = [-1] * rowlen
def helper(index, memo):
if index >= rowlen:
return 0
elif memo[index] != -1:
return memo[index]
else:
answer = -float("inf")
answer = max(answer, stones[index] - helper(index + 1, memo))
if index + 1 < rowlen:
answer = max(
answer,
stones[index] + stones[index + 1] - helper(index + 2, memo),
)
if index + 2 < rowlen:
answer = max(
answer,
stones[index]
+ stones[index + 1]
+ stones[index + 2]
- helper(index + 3, memo),
)
memo[index] = answer
return memo[index]
answer = helper(0, memo)
print(answer)
if answer < 0:
return "Bob"
elif answer > 0:
return "Alice"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
mem = {i: (0) for i in range(len(stoneValue) + 1)}
mem1, mem2, mem3 = 0, 0, 0
i = len(stoneValue) - 1
while i >= 0:
ans = stoneValue[i] - mem1
if i + 1 < len(stoneValue):
ans = max(ans, stoneValue[i] + stoneValue[i + 1] - mem2)
if i + 2 < len(stoneValue):
ans = max(
ans, stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] - mem3
)
mem3 = mem2
mem2 = mem1
mem1 = ans
i -= 1
if ans > 0:
return "Alice"
elif ans < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
dp = [0] * (len(stoneValue) + 1)
for i in range(len(stoneValue) - 1, -1, -1):
take = 0
dp[i] = -float("inf")
for k in range(3):
if i + k >= len(stoneValue):
continue
take += stoneValue[i + k]
dp[i] = max(dp[i], take - dp[i + k + 1])
if dp[0] < 0:
return "Bob"
if dp[0] > 0:
return "Alice"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
p1, p2, p3 = 0, 0, 0
post_sum = 0
for i in range(len(stoneValue) - 1, -1, -1):
val = stoneValue[i]
post_sum += val
best = post_sum - min([p1, p2, p3])
p1, p2, p3 = best, p1, p2
if p1 > post_sum - p1:
return "Alice"
elif p1 < post_sum - p1:
return "Bob"
else:
return "Tie"
def stoneGameIII(self, stoneValue: List[int]) -> str:
total = [0] * (len(stoneValue) + 4)
for i in range(len(stoneValue) - 1, -1, -1):
total[i] = total[i + 1] + stoneValue[i]
dp = [0] * len(total)
for i in range(len(stoneValue) - 1, -1, -1):
dp[i] = total[i] - min(dp[i + 1 : i + 4], default=0)
alice = dp[0]
bob = total[0] - alice
if alice > bob:
return "Alice"
elif alice < bob:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR RETURN STRING IF VAR BIN_OP VAR VAR RETURN STRING RETURN STRING VAR FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN STRING IF VAR VAR RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, cards):
lookup = {}
val = self.scoreGivenCards(cards, 0, True, lookup)
result = val - sum(cards) / 2
if result > 0:
return "Alice"
elif result < 0:
return "Bob"
else:
return "Tie"
def scoreGivenCards(self, cards, index, is_it_your_turn, lookup):
if index >= len(cards):
return 0
if is_it_your_turn:
if (index + 1, not is_it_your_turn) not in lookup:
lookup[index + 1, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 1, not is_it_your_turn, lookup
)
score_with_one = cards[index] + lookup[index + 1, not is_it_your_turn]
if (index + 2, not is_it_your_turn) not in lookup:
lookup[index + 2, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 2, not is_it_your_turn, lookup
)
score_with_two = (
sum(cards[index : index + 2]) + lookup[index + 2, not is_it_your_turn]
)
if (index + 3, not is_it_your_turn) not in lookup:
lookup[index + 3, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 3, not is_it_your_turn, lookup
)
score_with_three = (
sum(cards[index : index + 3]) + lookup[index + 3, not is_it_your_turn]
)
return max(score_with_one, score_with_two, score_with_three)
else:
if (index + 1, not is_it_your_turn) not in lookup:
lookup[index + 1, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 1, not is_it_your_turn, lookup
)
score_with_one = lookup[index + 1, not is_it_your_turn]
if (index + 2, not is_it_your_turn) not in lookup:
lookup[index + 2, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 2, not is_it_your_turn, lookup
)
score_with_two = lookup[index + 2, not is_it_your_turn]
if (index + 3, not is_it_your_turn) not in lookup:
lookup[index + 3, not is_it_your_turn] = self.scoreGivenCards(
cards, index + 3, not is_it_your_turn, lookup
)
score_with_three = lookup[index + 3, not is_it_your_turn]
return min(score_with_one, score_with_two, score_with_three) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, A: List[int]) -> str:
n = len(A)
dp = [-float("inf")] * n
A.reverse()
for i, x in enumerate(A):
adding = 0
for j in range(3):
if i - j >= 0:
adding += A[i - j]
dp[i] = max(dp[i], adding - (dp[i - j - 1] if i - j >= 1 else 0))
if dp[-1] < 0:
return "Bob"
return "Tie" if dp[-1] == 0 else "Alice" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN STRING RETURN VAR NUMBER NUMBER STRING STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stones: List[int]) -> str:
n = len(stones)
memo = {}
def solve(i):
if i == n:
return 0
if i in memo:
return memo[i]
ans, curr = -math.inf, 0
for j in range(i, min(n, i + 3)):
curr += stones[j]
ans = max(ans, curr - solve(j + 1))
memo[i] = ans
return ans
score = solve(0)
if score > 0:
return "Alice"
elif score < 0:
return "Bob"
else:
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, stoneValue: List[int]) -> str:
n = len(stoneValue)
dp = [float("-inf")] * n
dp[-1] = stoneValue[-1]
dp.append(0)
for i in range(n - 2, -1, -1):
temp = 0
for j in range(3):
if i + j + 1 > n:
break
temp += stoneValue[i + j]
dp[i] = max(dp[i], temp - dp[i + j + 1])
if dp[0] > 0:
return "Alice"
elif dp[0] < 0:
return "Bob"
return "Tie" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Alice and Bob continue their games with piles of stones. There are several stones arranged in a row, and each stone has an associated value which is an integer given in the array stoneValue.
Alice and Bob take turns, with Alice starting first. On each player's turn, that player can take 1, 2 or 3 stones from the first remaining stones in the row.
The score of each player is the sum of values of the stones taken. The score of each player is 0 initially.
The objective of the game is to end with the highest score, and the winner is the player with the highest score and there could be a tie. The game continues until all the stones have been taken.
Assume Alice and Bob play optimally.
Return "Alice" if Alice will win, "Bob" if Bob will win or "Tie" if they end the game with the same score.
Example 1:
Input: values = [1,2,3,7]
Output: "Bob"
Explanation: Alice will always lose. Her best move will be to take three piles and the score become 6. Now the score of Bob is 7 and Bob wins.
Example 2:
Input: values = [1,2,3,-9]
Output: "Alice"
Explanation: Alice must choose all the three piles at the first move to win and leave Bob with negative score.
If Alice chooses one pile her score will be 1 and the next move Bob's score becomes 5. The next move Alice will take the pile with value = -9 and lose.
If Alice chooses two piles her score will be 3 and the next move Bob's score becomes 3. The next move Alice will take the pile with value = -9 and also lose.
Remember that both play optimally so here Alice will choose the scenario that makes her win.
Example 3:
Input: values = [1,2,3,6]
Output: "Tie"
Explanation: Alice cannot win this game. She can end the game in a draw if she decided to choose all the first three piles, otherwise she will lose.
Example 4:
Input: values = [1,2,3,-1,-2,-3,7]
Output: "Alice"
Example 5:
Input: values = [-1,-2,-3]
Output: "Tie"
Constraints:
1 <= values.length <= 50000
-1000 <= values[i] <= 1000 | class Solution:
def stoneGameIII(self, nums: List[int]) -> str:
presum = list(itertools.accumulate(nums))
def sum(i, j):
if i == 0:
return presum[j]
return presum[j] - presum[i - 1]
n = len(nums)
if n > 1:
nums[-2] = max(sum(n - 2, n - 2), sum(n - 2, n - 1))
if n > 2:
nums[-3] = max(
nums[n - 3] + sum(n - 2, n - 1) - nums[-2],
sum(n - 3, n - 2),
sum(n - 3, n - 1),
)
for i in range(n - 4, -1, -1):
get_one = nums[i] + sum(i + 1, n - 1) - nums[i + 1]
get_two = sum(i, i + 1) + sum(i + 2, n - 1) - nums[i + 2]
get_three = sum(i, i + 2) + sum(i + 3, n - 1) - nums[i + 3]
nums[i] = max([get_one, get_two, get_three])
if nums[0] == sum(0, n - 1) / 2:
return "Tie"
elif nums[0] > sum(0, n - 1) / 2:
return "Alice"
return "Bob" | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, a):
mid_pointer = len(a)
dp = [0] * (2 * len(a) + 1)
cnt = mid_pointer
minus = 0
for i in range(len(a)):
if a[i] == "1":
cnt += 1
else:
cnt -= 1
if cnt <= mid_pointer:
minus += 1
dp[cnt] += 1
idx = mid_pointer
ans = 0
for i in range(len(a)):
ans += len(a) - i - minus
if a[i] == "1":
idx += 1
dp[idx] -= 1
minus += dp[idx]
pass
else:
minus -= 1
minus -= dp[idx]
idx -= 1
dp[idx] -= 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
ans = 0
cnt = 0
si = len(S)
dp = [0] * (2 * si + 2)
freq = [0] * (2 * si + 2)
freq[si] += 1
for i in S:
if i == "0":
cnt -= 1
else:
cnt += 1
if cnt - 1 + si >= 0:
dp[cnt + si] = dp[cnt - 1 + si] + freq[cnt - 1 + si]
ans += dp[cnt + si]
freq[cnt + si] += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
new = []
count = 0
cur_sum = 0
for i in S:
if i == "1":
cur_sum += 1
else:
cur_sum -= 1
new.append(cur_sum)
if cur_sum > 0:
count += 1
def mergesort(S):
if len(S) == 1:
return S, 0
mid = len(S) // 2
left, left_count = mergesort(S[:mid])
right, right_count = mergesort(S[mid:])
final, final_count = merge(left, right)
return final, left_count + final_count + right_count
def merge(l1, l2):
ct = 0
new = []
n1, n2 = 0, 0
m1, m2 = len(l1), len(l2)
while n1 < m1 and n2 < m2:
if l1[n1] < l2[n2]:
ct += m2 - n2
new.append(l1[n1])
n1 += 1
else:
new.append(l2[n2])
n2 += 1
new = new + l1[n1:] + l2[n2:]
return new, ct
st, ans = mergesort(new)
return ans + count | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
nums = [(1 if ch == "1" else -1) for ch in S]
pref_sum = [0] * len(nums)
pref_sum[0] = nums[0]
for i in range(1, len(nums)):
pref_sum[i] = pref_sum[i - 1] + nums[i]
pref_sum.insert(0, 0)
count = countSums(pref_sum)
return count
def countSums(pref_sum):
return countHelper(pref_sum, 0, len(pref_sum) - 1)
def countHelper(arr, left, right):
if left == right:
return 0
mid = (left + right) // 2
count1 = countHelper(arr, left, mid)
count2 = countHelper(arr, mid + 1, right)
count = count1 + count2
l_arr = arr[left : mid + 1]
r_arr = arr[mid + 1 : right + 1]
l_arr.sort()
r_arr.sort()
i = len(l_arr) - 1
j = len(r_arr) - 1
while i >= 0 and j >= 0:
if l_arr[i] < r_arr[j]:
count += i + 1
j -= 1
else:
i -= 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
n = len(S)
if "1" not in S:
return 0
elif "0" not in S:
return int(n * (n + 1) / 2)
def merge(arr, temp_arr, left, mid, right):
i = left
j = mid + 1
k = left
inv_count = 0
while i <= mid and j <= right:
if arr[i] <= arr[j]:
temp_arr[k] = arr[i]
k += 1
i += 1
else:
temp_arr[k] = arr[j]
inv_count += mid - i + 1
k += 1
j += 1
while i <= mid:
temp_arr[k] = arr[i]
k += 1
i += 1
while j <= right:
temp_arr[k] = arr[j]
k += 1
j += 1
for loop_var in range(left, right + 1):
arr[loop_var] = temp_arr[loop_var]
return inv_count
def _mergeSort(arr, temp_arr, left, right):
inv_count = 0
if left < right:
mid = (left + right) // 2
inv_count += _mergeSort(arr, temp_arr, left, mid)
inv_count += _mergeSort(arr, temp_arr, mid + 1, right)
inv_count += merge(arr, temp_arr, left, mid, right)
return inv_count
def mergeSort(arr, n):
temp_arr = [0] * n
return _mergeSort(arr, temp_arr, 0, n - 1)
pref = [(0) for i in range(n)]
for i in range(n):
if S[i] == "0":
pref[i] = -1
else:
pref[i] = 1
for i in range(1, n):
pref[i] += pref[i - 1]
count = 0
for i in range(n):
if pref[i] > 0:
count += 1
j = n - 1
i = 0
while i < j:
pref[i], pref[j] = pref[j], pref[i]
i += 1
j -= 1
return count + mergeSort(pref, n) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR RETURN NUMBER IF STRING VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
n = len(S)
ans = 0
zero = n
minus = 0
mp = [0] * (2 * n + 5)
cur = zero
for i in S:
if i == "0":
cur -= 1
else:
cur += 1
if cur <= zero:
minus += 1
mp[cur] += 1
for i in range(n):
ans += n - i - minus
if S[i] == "1":
mp[zero + 1] -= 1
zero += 1
minus += mp[zero]
else:
mp[zero - 1] -= 1
zero -= 1
minus -= 1
minus -= mp[zero + 1]
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
ans = 0
total = 0
below = 0
d = {(0): 1}
for ch in S:
if ch == "1":
total += 1
below += d.get(total - 1, 0)
else:
total -= 1
below -= d.get(total, 0)
ans += below
d[total] = d.get(total, 0) + 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def __init__(self):
self.invCnt = 0
def merge(self, arr, s, e, mid):
arr1 = arr[s : mid + 1]
arr2 = arr[mid + 1 : e + 1]
imax = mid - s + 1
jmax = e - mid
i = 0
j = 0
k = s
while i < imax and j < jmax:
if arr1[i] >= arr2[j]:
arr[k] = arr1[i]
i += 1
else:
self.invCnt += imax - i
arr[k] = arr2[j]
j += 1
k += 1
while i < imax:
arr[k] = arr1[i]
i += 1
k += 1
while j < jmax:
arr[k] = arr2[j]
j += 1
k += 1
def mergeSort(self, arr, s, e):
if s < e:
mid = (s + e) // 2
self.mergeSort(arr, s, mid)
self.mergeSort(arr, mid + 1, e)
self.merge(arr, s, e, mid)
def inversionCount(self, arr, n):
self.mergeSort(arr, 0, n - 1)
return self.invCnt
def countSubstring(self, S):
N = len(S)
dp = [0] * N
cnt = 0
i = 0
for v in S:
if v == "0":
if i == 0:
dp[i] = -1
else:
dp[i] = dp[i - 1] - 1
elif i == 0:
dp[i] = 1
else:
dp[i] = dp[i - 1] + 1
if dp[i] > 0:
cnt += 1
i += 1
self.inversionCount(dp, i)
return self.invCnt + cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
n = len(s)
dp = [0] * (2 * n + 1)
minius, count = 0, n
for i in range(n):
if s[i] == "0":
count -= 1
else:
count += 1
if count <= n:
minius += 1
dp[count] += 1
ans = 0
ind = n
for i in range(n):
ans += n - i - minius
if s[i] == "1":
ind += 1
dp[ind] -= 1
minius += dp[ind]
else:
minius -= 1
minius -= dp[ind]
ind -= 1
dp[ind] -= 1
return ans
if __name__ == "__main__":
for _ in range(int(input())):
s = input()
print(Solution().countSubstring(s)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
res = 0
levels = {(0): [1, 0]}
level = 0
for s in S:
x = 1 if s == "1" else -1
newLevel = level - x
if newLevel not in levels.keys():
if x == -1:
levels[newLevel] = [1, 0]
else:
levels[newLevel] = [1, sum(levels[level])]
elif x == -1:
levels[newLevel][0] += 1
else:
levels[newLevel] = [levels[newLevel][0] + 1, sum(levels[level])]
res += levels[newLevel][1]
level = newLevel
return res | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR STRING NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.
Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"
Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)
Constraints:
1 < |S| < 10^{5}
|S| denotes the length of the string S | class Solution:
def countSubstring(self, S):
c = 0
ans = 0
n = len(S)
freq = []
dp = []
for i in range(2 * n + 2):
freq.append(0)
dp.append(0)
freq[n] += 1
for i in S:
if i == "0":
c -= 1
else:
c += 1
if c - 1 + n >= 0:
dp[c + n] = dp[c - 1 + n] + freq[c - 1 + n]
ans += dp[c + n]
freq[c + n] += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
t = [[(-1) for i in range(n + 1)] for _ in range(n + 1)]
def rever(S):
a = list(S)
a = a[::-1]
return "".join(a)
def lcs(x, y, n, m):
for i in range(n + 1):
for j in range(m + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, n + 1):
for j in range(1, m + 1):
if x[i - 1] == y[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[n][m]
s1 = rever(S)
return lcs(S, s1, n, n) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL STRING VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, str):
n = len(str)
L = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
L[i][i] = 1
for cl in range(2, n + 1):
for i in range(n - cl + 1):
j = i + cl - 1
if str[i] == str[j] and cl == 2:
L[i][j] = 2
elif str[i] == str[j]:
L[i][j] = L[i + 1][j - 1] + 2
else:
L[i][j] = max(L[i][j - 1], L[i + 1][j])
return L[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
Srev = S[::-1]
dp = [[(0) for j in range(len(Srev) + 1)] for i in range(len(S) + 1)]
for i in range(1, len(dp)):
for j in range(1, len(dp[0])):
s1_sub = S[:i]
s2_sub = Srev[:j]
if s1_sub[-1] == s2_sub[-1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
s1 = S[::-1]
n = len(S)
dp = [[(0) for i in range(n + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, n + 1):
if s1[i - 1] == S[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
return dp[n][n] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def pali(self, s, l, r):
if (l, r) in self.d:
return self.d[l, r]
if l == r:
self.d[l, r] = 1
return self.d[l, r]
if l + 1 == r and s[l] == s[r]:
self.d[l, r] = 2
return self.d[l, r]
if s[l] == s[r]:
self.d[l, r] = self.pali(s, l + 1, r - 1) + 2
return self.d[l, r]
self.d[l, r] = max(self.pali(s, l + 1, r), self.pali(s, l, r - 1))
return self.d[l, r]
def longestPalinSubseq(self, S):
self.ans = ""
self.d = {}
return self.pali(S, 0, len(S) - 1) | CLASS_DEF FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | def ispal(s):
return s == s[::-1]
class Solution:
def longestPalinSubseq(self, S):
dp = {}
def f(i, j):
if (i, j) in dp:
return dp[i, j]
if i > j:
return 0
if i == j:
return 1
if S[i] == S[j]:
dp[i, j] = 2 + f(i + 1, j - 1)
else:
dp[i, j] = max(f(i + 1, j), f(i, j - 1))
return dp[i, j]
return f(0, len(S) - 1) | FUNC_DEF RETURN VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def solveTab(self, a, b):
dp = [[(0) for col in range(len(b) + 1)] for row in range(len(a) + 1)]
for i in range(len(a) - 1, -1, -1):
for j in range(len(b) - 1, -1, -1):
ans = 0
if a[i] == b[j]:
ans = 1 + dp[i + 1][j + 1]
else:
ans = max(dp[i + 1][j], dp[i][j + 1])
dp[i][j] = ans
return dp[0][0]
def longestPalinSubseq(self, text1):
text2 = text1[::-1]
return self.solveTab(text1, text2) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(s)
curr, prev = [0] * n, [0] * n
for i in range(n - 1, -1, -1):
curr[i] = 1
for j in range(i + 1, n):
if s[i] == s[j]:
curr[j] = prev[j - 1] + 2
else:
curr[j] = max(prev[j], curr[j - 1])
curr, prev = prev, curr
return prev[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def f(self, s1, s2, ind1, ind2, dp):
if ind1 < 0 or ind2 < 0:
return 0
if dp[ind1][ind2] != -1:
return dp[ind1][ind2]
if s1[ind1] == s2[ind2]:
dp[ind1][ind2] = 1 + self.f(s1, s2, ind1 - 1, ind2 - 1, dp)
return dp[ind1][ind2]
dp[ind1][ind2] = 0 + max(
self.f(s1, s2, ind1, ind2 - 1, dp), self.f(s1, s2, ind1 - 1, ind2, dp)
)
return dp[ind1][ind2]
def longestPalinSubseq(self, S):
x = len(S)
dp = [[(-1) for i in range(x)] for j in range(x)]
s2 = S[::-1]
return self.f(S, s2, x - 1, x - 1, dp) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s1):
x = len(s1)
y = x
s2 = s1[::-1]
dp = [[(-1) for i in range(y)] for j in range(x)]
def answer(i, j, s1, s2):
if i == -1 or j == -1:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
dp[i][j] = 1 + answer(i - 1, j - 1, s1, s2)
return dp[i][j]
else:
dp[i][j] = max(answer(i, j - 1, s1, s2), answer(i - 1, j, s1, s2))
return dp[i][j]
return answer(x - 1, y - 1, s1, s2) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | def lcs(X, Y, m, n, dp):
if m == 0 or n == 0:
return 0
if dp[m][n] != -1:
return dp[m][n]
if X[m - 1] == Y[n - 1]:
dp[m][n] = 1 + lcs(X, Y, m - 1, n - 1, dp)
return dp[m][n]
else:
dp[m][n] = max(lcs(X, Y, m, n - 1, dp), lcs(X, Y, m - 1, n, dp))
return dp[m][n]
class Solution:
def longestPalinSubseq(self, S):
s2 = S
s2 = s2[::-1]
n = len(S)
dp = [[(-1) for i in range(n + 1)] for i in range(n + 1)]
return lcs(s2, S, n, n, dp) | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
def lps(s, left, right, dp):
key = left, right
if left > right:
return 0
if left == right:
return 1
if key in dp:
return dp[key]
if s[left] == s[right]:
dp[key] = 2 + lps(s, left + 1, right - 1, dp)
return dp[key]
else:
dp[key] = max(lps(s, left + 1, right, dp), lps(s, left, right - 1, dp))
return dp[key]
dp = dict()
res = lps(S, 0, len(S) - 1, dp)
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def f(self, i, j, n, s, s1, dp):
if i == n or j == n:
return 0
if dp[i][j] != -1:
return dp[i][j]
ans = 0
if s[i] == s1[j]:
ans = 1 + self.f(i + 1, j + 1, n, s, s1, dp)
else:
ans = max(self.f(i + 1, j, n, s, s1, dp), self.f(i, j + 1, n, s, s1, dp))
dp[i][j] = ans
return ans
def longestPalinSubseq(self, S):
s1 = s[::-1]
dp = [[(-1) for i in range(len(s1))] for j in range(len(S))]
return self.f(0, 0, len(S), S, s1, dp) | CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
s = S
dp = [([1] * len(S)) for i in range(len(S))]
for i in range(len(S) - 1):
if s[i] == s[i + 1]:
dp[i][i + 1] = 2
else:
dp[i][i + 1] = 1
for l in range(3, len(S) + 1):
for i in range(len(S)):
j = i + l - 1
if j >= len(S):
continue
if s[i] == s[j]:
dp[i][j] = max(dp[i + 1][j - 1] + 2, dp[i][j])
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1], dp[i][j])
return dp[0][len(S) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
def reverse(s):
str = ""
for i in s:
str = i + str
return str
def lcs(x, y, s1, s2):
T = [([0] * (y + 1)) for i in range(x + 1)]
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
T[i][j] = 1 + T[i - 1][j - 1]
else:
T[i][j] = max(T[i - 1][j], T[i][j - 1])
return T[x][y]
return lcs(len(S), len(S), S, reverse(S)) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def solve(self, S, i, j, dp):
if i == j:
dp[i][j] = 1
return 1
if i + 1 == j and S[i] == S[j]:
dp[i][j] = 2
return 2
if dp[i][j] != -1:
return dp[i][j]
if S[i] == S[j]:
dp[i][j] = self.solve(S, i + 1, j - 1, dp) + 2
else:
dp[i][j] = max(self.solve(S, i + 1, j, dp), self.solve(S, i, j - 1, dp))
return dp[i][j]
def longestPalinSubseq(self, S):
dp = [[(-1) for each in range(len(S) + 1)] for each1 in range(len(S) + 1)]
a = self.solve(S, 0, len(S) - 1, dp)
return a | CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
t = [([0] * (n + 1)) for _ in range(n + 2)]
S1 = S[::-1]
for i in range(1, n + 1):
for j in range(1, n + 1):
if S[i - 1] == S1[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[n][n] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s):
n = len(s)
str1 = s
str2 = s[::-1]
res = ""
l = [[(0) for i in range(n + 1)] for i in range(n + 1)]
for i in range(n):
for j in range(n):
if str1[i] == str2[j]:
l[i + 1][j + 1] = 1 + l[i][j]
else:
l[i + 1][j + 1] = max(l[i][j + 1], l[i + 1][j])
return l[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def LPS(self, string, i, j, dp):
if i > len(string) - 1 or j < 0 or i > j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if i == j:
return 1
if string[i] == string[j]:
ans = 2 + self.LPS(string, i + 1, j - 1, dp)
else:
left = self.LPS(string, i + 1, j, dp)
right = self.LPS(string, i, j - 1, dp)
ans = max(left, right)
dp[i][j] = ans
return ans
def longestPalinSubseq(self, S):
string = S
n = len(string)
dp = [[(-1) for i in range(n)] for j in range(n)]
return self.LPS(string, 0, n - 1, dp) | CLASS_DEF FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s):
s1 = s
s2 = s1[::-1]
dp = [[(-1) for i in range(len(s2))] for j in range(len(s1))]
return self.helper(0, 0, s1, s2, dp)
def helper(self, i, j, s1, s2, dp):
if i == len(s1) or j == len(s2):
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
dp[i][j] = 1 + self.helper(i + 1, j + 1, s1, s2, dp)
return dp[i][j]
else:
dp[i][j] = max(
self.helper(i + 1, j, s1, s2, dp), self.helper(i, j + 1, s1, s2, dp)
)
return dp[i][j] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.