description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): arr = [(a[i], b[i]) for i in range(n)] arr.sort(key=lambda x: abs(x[0] - x[1]), reverse=True) t = 0 for i in range(n): if arr[i][0] >= arr[i][1]: if x > 0: t += arr[i][0] x -= 1 else: t += arr[i][1] y -= 1 elif y > 0: t += arr[i][1] y -= 1 else: t += arr[i][0] x += 1 return t
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): ans = 0 arrs = [] for i in range(n): temp = [] temp.append(abs(a[i] - b[i])) temp.append(a[i]) temp.append(b[i]) arrs.append(temp) arrs.sort() for i in range(n - 1, -1, -1): if arrs[i][1] >= arrs[i][2]: if x > 0: ans += arrs[i][1] x -= 1 else: ans += arrs[i][2] elif y > 0: ans += arrs[i][2] y -= 1 else: ans += arrs[i][1] x -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): l = [] for i in range(n): l.append([a[i], b[i], abs(a[i] - b[i])]) l.sort(reverse=True, key=lambda x: x[2]) res = 0 for i in range(n): if x <= 0: res += l[i][1] elif y <= 0: res += l[i][0] elif l[i][0] > l[i][1]: res += l[i][0] x -= 1 else: res += l[i][1] y -= 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): diff = [(a[i] - b[i], a[i], b[i]) for i in range(n)] diff.sort(reverse=True) r_count, a_count = 0, 0 for d, ar, br in diff: if d >= 0 and r_count < x: r_count += 1 elif d < 0 and a_count < y: a_count += 1 elif r_count < x: r_count += 1 else: a_count += 1 total_tip = sum(ar for d, ar, br in diff[:r_count]) total_tip += sum(br for d, ar, br in diff[r_count : r_count + a_count]) return total_tip
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def solve(self, a, b, x, y, i, dp): if i >= len(a): return 0 if x == 0 and y == 0: return 0 if (i, x, y) in dp: return dp[i, x, y] if x > 0 and y > 0: dp[i, x, y] = max( a[i] + self.solve(a, b, x - 1, y, i + 1, dp), b[i] + self.solve(a, b, x, y - 1, i + 1, dp), ) return dp[i, x, y] elif x > 0: dp[i, x, y] = a[i] + self.solve(a, b, x - 1, y, i + 1, dp) return dp[i, x, y] else: dp[i, x, y] = b[i] + self.solve(a, b, x, y - 1, i + 1, dp) return dp[i, x, y] def maxTip(self, a, b, n, x, y): arr = [] for i in range(n): arr.append([a[i], b[i]]) arr.sort(key=lambda x: abs(x[0] - x[1])) arr.reverse() i = 0 ans = 0 while x > 0 and y > 0 and i < n: if arr[i][1] >= arr[i][0]: ans += arr[i][1] y -= 1 else: ans += arr[i][0] x -= 1 i += 1 while i < n and x > 0: ans += arr[i][0] x -= 1 i += 1 while i < n and y > 0: ans += arr[i][1] y -= 1 i += 1 return ans
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): array, tips = [], 0 for i in range(0, n): array.append([a[i], b[i], abs(a[i] - b[i])]) array.sort(reverse=True, key=lambda x: x[2]) for i in range(0, n): if array[i][0] > array[i][1] and x != 0: tips += array[i][0] x -= 1 elif array[i][0] < array[i][1] and y != 0: tips += array[i][1] y -= 1 elif x == 0: tips += array[i][1] else: tips += array[i][0] return tips
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): ans = 0 i = 0 r = 0 an = 0 s = list(zip(a, b)) s = sorted(s, key=lambda x: abs(x[0] - x[1]), reverse=True) while i < n and (r < x and an < y): if s[i][0] > s[i][1]: ans += s[i][0] r += 1 else: ans += s[i][1] an += 1 i += 1 if an < y: while i < n and an < y: ans += s[i][1] an += 1 i += 1 else: while i < n and r < x: ans += s[i][0] r += 1 i += 1 return ans if __name__ == "__main__": tc = int(input()) while tc > 0: n, x, y = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) b = list(map(int, input().strip().split())) ans = Solution().maxTip(a, b, n, x, y) print(ans) tc -= 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): ans, vec = 0, [(0, 0)] * n for i in range(n): vec[i] = a[i] - b[i], i vec = sorted(vec, reverse=True) lt, rt, l, r = 0, 0, 0, n - 1 while lt + rt < n and lt < x and rt < y: if vec[l][0] >= abs(vec[r][0]): ans += a[vec[l][1]] lt += 1 l += 1 else: ans += b[vec[r][1]] r -= 1 rt += 1 while lt + rt < n and lt < x: ans += a[vec[l][1]] l += 1 lt += 1 while lt + rt < n and rt < y: ans += b[vec[r][1]] r -= 1 rt += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def util(self, a, b, n, x, y, index, hm): if index >= n or x < 0 or y < 0: return 0 key = str(index) + "_" + str(x) + "_" + str(y) if key in hm: return hm[key] if x > 0 and y > 0: dp = max( a[index] + self.util(a, b, n, x - 1, y, index + 1, hm), b[index] + self.util(a, b, n, x, y - 1, index + 1, hm), ) elif x > 0: dp = a[index] + self.util(a, b, n, x - 1, y, index + 1, hm) elif y > 0: dp = b[index] + self.util(a, b, n, x, y - 1, index + 1, hm) hm[key] = dp return dp def maxTip(self, a, b, n, x, y): diff = [] for i in range(n): diff.append([abs(a[i] - b[i]), i]) diff = sorted(diff) diff = diff[::-1] tip = 0 for j in diff: index = j[1] if x > 0 and y > 0: tip += max(a[index], b[index]) if a[index] >= b[index]: x -= 1 else: y -= 1 elif x > 0 and y <= 0: tip += a[index] x -= 1 else: tip += b[index] y -= 1 return tip
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR VAR RETURN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): lis = [] for i in range(n): lis.append([abs(a[i] - b[i]), i]) lis.sort(key=lambda x: x[0], reverse=True) ans = 0 for i in lis: idx = i[1] if x > 0 and y > 0: if a[idx] > b[idx]: ans += a[idx] x -= 1 elif a[idx] == b[idx]: if x > y: ans += a[idx] x -= 1 else: ans += a[idx] y -= 1 else: ans += b[idx] y -= 1 elif x > 0: ans += a[idx] x -= 1 elif y > 0: ans += b[idx] y -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): diff = [] for i in range(n): diff.append((abs(a[i] - b[i]), a[i], b[i])) diff = sorted(diff, key=lambda diff: diff[0], reverse=True) m = 0 for val in diff: if val[1] > val[2] and x > 0: m += val[1] x -= 1 elif val[1] < val[2] and y > 0: m += val[2] y -= 1 elif y > x: m += val[2] y -= 1 else: m += val[1] x -= 1 return m
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): diff = [[a[i] - b[i], i] for i in range(n)] diff.sort() res = 0 for i in range(n): if y > 0 and n - i > x: res += b[diff[i][1]] y -= 1 elif y > 0 and n - i <= x and diff[i][0] < 0: res += b[diff[i][1]] y -= 1 elif y > 0 and n - i <= x and diff[i][0] >= 0: res += a[diff[i][1]] x -= 1 else: res += a[diff[i][1]] x -= 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): dp = [] for i in range(n): dp.append([abs(a[i] - b[i]), a[i], b[i]]) dp.sort(reverse=True) ans = 0 for i in range(n): p, q = dp[i][1], dp[i][2] if x == 0: ans += q elif y == 0: ans += p elif p > q: ans += p x -= 1 else: ans += q y -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): temp = [] result = 0 for i in range(n): temp.append([abs(a[i] - b[i]), i]) temp.sort(reverse=True) for elem in temp: if a[elem[1]] > b[elem[1]]: if x > 0: result += a[elem[1]] x -= 1 else: result += b[elem[1]] y -= 1 elif y > 0: result += b[elem[1]] y -= 1 else: result += a[elem[1]] x -= 1 return result
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): mx = 0 diff_contribution = [abs(a[index] - b[index]) for index in range(n)] diff_contribution = sorted(enumerate(diff_contribution), key=lambda d: -d[1]) diff_index = [d[0] for d in diff_contribution] for index_value in diff_index: if a[index_value] >= b[index_value] and x: mx += a[index_value] x -= 1 elif a[index_value] < b[index_value] and y: mx += b[index_value] y -= 1 elif not x and y: mx += b[index_value] y -= 1 elif not y and x: mx += a[index_value] x -= 1 return mx
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): ix = sorted(list(range(n)), key=lambda i: abs(a[i] - b[i]), reverse=True) ans = 0 for i in ix: if x == 0: ans += b[i] y -= 1 elif y == 0: ans += a[i] x -= 1 elif a[i] >= b[i]: ans += a[i] x -= 1 else: ans += b[i] y -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): arr = [(a[i] - b[i], a[i], b[i]) for i in range(n)] arr.sort() summa = 0 while arr and x and arr[-1][0] >= 0: summa += arr.pop()[1] x -= 1 j = len(arr) - y for _ in range(j): summa += arr.pop()[1] summa += sum([item[2] for item in arr]) return summa
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): arr = [] one = 0 two = 0 for i in range(n): arr.append([a[i], b[i], a[i] - b[i], b[i] - a[i]]) if arr[i][2] < 0: two += 1 else: one += 1 arr.sort(key=lambda x: x[2], reverse=True) cost = 0 i = 0 a = x if x + y > n: a = x - (x + y - n) while a and i < n: cost += arr[i][0] a -= 1 i += 1 a = x + y - n while a and i < n: if arr[i][0] > arr[i][1]: cost += arr[i][0] else: cost += arr[i][1] i += 1 a -= 1 while y and i < n: cost += arr[i][1] y -= 1 i += 1 return cost
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received n orders. The amount of tips may differ when handled by different waiters, if Rahul takes the i_{th} order, he would be tipped a_{i} rupees and if Ankit takes this order, the tip would be b_{i} rupees. In order to maximize the total tip value they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints Rahul cannot take more than x orders and Ankit cannot take more than y orders. It is guaranteed that x + y is greater than or equal to n, which means that all the orders can be handled by either Rahul or Ankit. Find out the maximum possible amount of total tip money after processing all the orders. Example 1: Input: n = 5, x = 3, y = 3 a[] = {1, 2, 3, 4, 5} b[] = {5, 4, 3, 2, 1} Output: 21 Explanation: Rahul will serve 3rd, 4th and 5th order while Ankit will serve the rest. Example 2: Input: n = 8, x = 4, y = 4 a[] = {1, 4, 3, 2, 7, 5, 9, 6} b[] = {1, 2, 3, 6, 5, 4, 9, 8} Output: 43 Explanation: Rahul will serve 1st, 2nd, 5th and 6th order while Ankit will serve the rest. Your Task: You don't need to read input or print anything. Your task is to complete the function maxTip() which takes array a[ ], array b[ ], n, x and y as input parameters and returns an integer denoting the answer. Expected Time Complexity: O(n*logn) Expected Auxiliary Space: O(n) Constraints: 1 ≤ n ≤ 10^{5} 1 ≤ x, y ≤ N x + y ≥ n 1 ≤ a[i], b[i] ≤ 10^{5}
class Solution: def maxTip(self, a, b, n, x, y): tip = 0 d = list() for i in range(n): d.append([abs(a[i] - b[i]), i]) d.sort(reverse=True) for i in range(n): if a[d[i][1]] >= b[d[i][1]]: if x > 0: tip += a[d[i][1]] x -= 1 elif y > 0: tip += b[d[i][1]] y -= 1 elif b[d[i][1]] >= a[d[i][1]]: if y > 0: tip += b[d[i][1]] y -= 1 elif x > 0: tip += a[d[i][1]] x -= 1 return tip
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): prev2, prev1 = 0, a[0] for i in range(1, n): not_pick = prev1 pick = a[i] + prev2 prev2 = prev1 prev1 = max(not_pick, pick) return prev1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return 0 if a[0] < 0 else a[0] include = [0] * len(a) exclude = [0] * len(a) include[0], include[1] = a[0], a[1] if include[0] < 0: include[0] = 0 exclude[0], exclude[1] = 0, include[0] if a[1] < 0: include[1] = 0 for j in range(2, len(a), 1): inc = max(exclude[j - 2], include[j - 2]) exc = max(include[j - 1], exclude[j - 1]) if a[j] < 0: include[j], exclude[j] = exc, exc else: include[j], exclude[j] = inc + a[j], exc return max(include[-1], exclude[-1])
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def solve(self, n, arr, memo): if n == 0: return arr[n] if n < 0: return 0 if memo[n] != -1: return memo[n] take = self.solve(n - 2, arr, memo) + arr[n] no = self.solve(n - 1, arr, memo) memo[n] = max(take, no) return memo[n] def FindMaxSum(self, a, n): if n == 1: return a[0] memo = [-1] * (n + 1) return self.solve(n - 1, a, memo)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
import sys class Solution: def FindMaxSum(self, a, n): import sys sys.setrecursionlimit(10**9) dist = [-1] * n self.solve(n - 1, a, dist) return dist[n - 1] def solve(self, ind, arr, dp): if ind == 0: dp[ind] = arr[ind] return arr[ind] if ind < 0: return 0 if dp[ind] != -1: return dp[ind] pick = arr[ind] + self.solve(ind - 2, arr, dp) npick = self.solve(ind - 1, arr, dp) dp[ind] = max(pick, npick) return dp[ind]
IMPORT CLASS_DEF FUNC_DEF IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return a[0] prevMax = a[0] currMax = max(a[0], a[1]) prev = a[1] for i in range(2, n): curr = a[i] + prevMax prevMax = max(prevMax, prev) currMax = max(currMax, curr) prev = curr return currMax
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): takecurr = a[0] best = 0 for i in range(1, n): takecurr, best = best + a[i], max(best, takecurr) return max(best, takecurr)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, nums, n): prev2 = 0 prev = nums[0] for i in range(1, n): fh = nums[i] if i > 1: fh += prev2 sh = 0 + prev prev2 = prev prev = max(fh, sh) return prev
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): self.a = a self.n = n dp = [0] * (n + 1) for i in range(n, -1, -1): if i >= n: dp[i] = 0 continue way1 = a[i] + (0 if i + 2 > n else dp[i + 2]) way2 = dp[i + 1] dp[i] = max(way1, way2) return dp[0] def steal(self, i): if i >= self.n: return 0 way1 = self.a[i] + self.steal(i + 2) way2 = self.steal(i + 1) return max(way1, way2)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, N): def solveUtil(ind, arr, dp): if dp[ind] != -1: return dp[ind] if ind == 0: return arr[ind] if ind < 0: return 0 pick = arr[ind] + solveUtil(ind - 2, arr, dp) nonPick = 0 + solveUtil(ind - 1, arr, dp) dp[ind] = max(pick, nonPick) return dp[ind] dp = [(-1) for i in range(n)] return solveUtil(n - 1, arr, dp)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): rob1, rob2 = 0, 0 for n in a: temp = max(n + rob1, rob2) rob1 = rob2 rob2 = temp return rob2
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if len(a) == 1: return a[0] prevprev = a[0] prev = max(a[1], a[0]) for i in range(2, n): curr = max(prev, prevprev + a[i]) prevprev = prev prev = curr return prev
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, n): dp = [-1] * n return self.rec(n - 1, arr, dp) def rec(self, i, arr, dp): if i < 0: return 0 if i == 0: return arr[0] take = arr[i] if dp[i - 2] != -1: take += dp[i - 2] else: val = self.rec(i - 2, arr, dp) dp[i - 2] = val take += val noTake = 0 if dp[i - 1] != -1: noTake += dp[i - 1] else: val = self.rec(i - 1, arr, dp) dp[i - 1] = val noTake += val return max(take, noTake)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, n): a = arr[0] ex = 0 for i in range(1, n): nex = max(a, ex) a = ex + arr[i] ex = nex return max(a, ex)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): prev = a[0] prev2 = 0 for i in range(1, n): take = a[i] if i > 1: take += prev2 nontake = prev curr = max(take, nontake) prev2 = prev prev = curr return prev
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, n): prev1 = arr[0] prev2 = 0 curr = 0 for i in range(1, n): pick = arr[i] if i > 1: pick = pick + prev2 npick = 0 + prev1 curr = max(pick, npick) prev2 = prev1 prev1 = curr return prev1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return a[0] if n == 2: return max(a[0], a[1]) first = a[0] second = max(a[0], a[1]) i = 2 while i < n: temp = second second = max(a[i] + first, second) first = temp i += 1 return second
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): dp = [-1] * n def recHelper(k): if k == 0: return a[0] if k == 1: return max(a[0], a[1]) if dp[k] != -1: return dp[k] dp[k] = max(recHelper(k - 1), a[k] + recHelper(k - 2)) return dp[k] return recHelper(n - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def solve(self, idx, a, n): if idx >= n: return 0 incl = a[idx] + self.solve(idx + 2, a, n) excl = self.solve(idx + 1, a, n) return max(incl, excl) def solve_memo(self, idx, a, n, F): if idx >= n: return 0 if F[idx] != -1: return F[idx] incl = a[idx] + self.solve_memo(idx + 2, a, n, F) excl = self.solve_memo(idx + 1, a, n, F) F[idx] = max(incl, excl) return F[idx] def solve_tab(self, a, n): F = [(0) for _ in range(n + 2)] for idx in range(n - 1, -1, -1): incl = a[idx] + F[idx + 2] excl = F[idx + 1] F[idx] = max(incl, excl) return F[0] def solve_opt(self, a, n): prev2 = prev1 = 0 for idx in range(n - 1, -1, -1): incl = a[idx] + prev2 excl = prev1 ans = max(incl, excl) prev2 = prev1 prev1 = ans return prev1 def FindMaxSum(self, a, n): return self.solve_opt(a, n)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, nums, n): f = 0 s = 0 x = 0 l = len(nums) for i in range(0, l): x = max(nums[i] + f, s) f = s s = x return x
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return a[0] rob1 = a[0] rob2 = max(a[0], a[1]) for i in a[2:]: rob1, rob2 = rob2, max(i + rob1, rob2) return rob2
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
from sys import maxsize class Solution: def FindMaxSum(self, arr, n): if n == 1: return arr[0] tab = [(0) for i in range(n)] tab[0], tab[1] = arr[0], max(arr[0], arr[1]) for i in range(2, n): tab[i] = max(tab[i - 1], arr[i] + tab[i - 2]) return tab[-1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return a[0] if n == 2: return max(a[0], a[1]) d = [a[0], a[1], a[0] + a[2]] for i in range(3, n): d.append(max(d[len(d) - 2] + a[i], d[len(d) - 3] + a[i])) return max(d[len(d) - 1], d[len(d) - 2])
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): incl = 0 excl = 0 for i in range(n): curr = max(a[i] + excl, incl) excl = incl incl = curr return curr
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): arr = a left = arr[0] if n == 1: return left right = max(arr[0], arr[1]) for i in range(2, n): valone = left + arr[i] left = right if valone > right: right = valone return right
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): inc = exc = 0 for i in a: new_exc = max(exc, inc) inc = i + exc exc = new_exc return max(inc, exc)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 0: return 0 if n == 1: return a[0] if n == 2: return max(a[0], a[1]) prev2 = 0 prev1 = a[0] for i in range(1, n): ans = max(a[i] + prev2, prev1) prev2 = prev1 prev1 = ans return prev1
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def maxSum(self, i, a, n): if i == n - 1: return a[i] if i >= n: return 0 curr_sum = max(a[i] + self.maxSum(i + 2, a, n), self.maxSum(i + 1, a, n)) return curr_sum def FindMaxSum(self, a, n): prev = 0 prev_2 = 0 current = 0 for i in range(n - 1, -1, -1): current = max(a[i] + prev_2, 0 + prev) prev_2 = prev prev = current return prev
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def solve(self, a, index, n, dp): if index >= n: return 0 if dp[index] != -1: return dp[index] incl = self.solve(a, index + 2, n, dp) ans = incl + a[index] excl = self.solve(a, index + 1, n, dp) dp[index] = max(ans, excl) return dp[index] def FindMaxSum(self, a, n): index = 0 dp = [(-1) for i in range(n + 1)] ans = self.solve(a, index, n, dp) return ans
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
import sys class Solution: def __init__(self): self.dp = {} def findMaxSumUtil(self, a, n, m): if m >= n: return 0 if m in self.dp: return self.dp[m] ans = max( a[m] + self.findMaxSumUtil(a, n, m + 2), self.findMaxSumUtil(a, n, m + 1) ) self.dp[m] = ans return ans def FindMaxSum(self, a, n): return self.findMaxSumUtil(a, n, 0)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): dp = [(0) for _ in range(n + 1)] dp[1] = a[0] for j in range(2, n + 1): dp[j] = max(a[j - 1] + dp[j - 2], dp[j - 1]) return dp[n]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def solve1(self, a, n, dp): dp[0] = a[0] for i in range(1, n): inc = dp[i - 2] + a[i] exc = dp[i - 1] dp[i] = max(inc, exc) return dp[n - 1] def FindMaxSum(self, a, n): dp = [(0) for _ in range(n)] return self.solve1(a, n, dp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 1: return a[0] elif n == 2: return max(a) r = [a[0], a[1], a[0] + a[2]] for i in range(3, n): r.append(a[i] + max(r[i - 2], r[i - 3])) return max(r)
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): dp = [-1] * n dp[0] = a[0] for i in range(1, n): if i - 2 < 0: temp = 0 else: temp = dp[i - 2] pick = a[i] + temp non_pick = dp[i - 1] dp[i] = max(pick, non_pick) return dp[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): second = 0 first = a[0] for i in range(2, n + 1): second, first = first, max(a[i - 1] + second, first) return first
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 0: return 0 elif n == 1: return a[0] elif n == 2: return max(a[0], a[1]) else: b = [] b.append(a[0]) if a[0] == a[1]: b.append(a[0]) else: b.append(max(a[0], a[1])) for i in range(2, n): b.append(max(b[i - 2] + a[i], b[i - 1])) return b[-1]
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if len(a) == 0: return 0 if len(a) == 1: return a[0] table = [(0) for x in range(n)] table[0] = a[0] table[1] = max(a[0], a[1]) for i in range(2, n): table[i] = max(table[i - 1], table[i - 2] + a[i]) return table[n - 1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR BIN_OP VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, n): p1 = arr[0] p2 = 0 for i in range(1, n): take = arr[i] if i > 1: take += p2 noTake = p1 + 0 p2 = p1 p1 = max(take, noTake) return p1 def rec(self, i, arr, dp): if i < 0: return 0 if i == 0: return arr[0] take = arr[i] if dp[i - 2] != -1: take += dp[i - 2] else: val = self.rec(i - 2, arr, dp) dp[i - 2] = val take += val noTake = 0 if dp[i - 1] != -1: noTake += dp[i - 1] else: val = self.rec(i - 1, arr, dp) dp[i - 1] = val noTake += val return max(take, noTake)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def findAns(self, a, i, j): if i > j: return 0 return max(a[i] + self.findAns(a, i + 2, j), self.findAns(a, i + 1, j)) def FindMaxSum(self, a, n): if n == 1: return a[0] prev = a[0] prev2 = 0 for i in range(1, n): cur = max(a[i] + prev2, prev) prev2 = prev prev = cur return prev
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): include = a[0] exclude = 0 for x in a[1:]: temp = include include = max(exclude + x, include) exclude = temp return max(include, exclude)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): def find_max(a, i, dp): if i >= len(a): return 0 if dp[i] != -1: return dp[i] c = a[i] + find_max(a, i + 2, dp) b = find_max(a, i + 1, dp) dp[i] = max(c, b) return dp[i] dp = [-1] * n return find_max(a, 0, dp)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, nums, n): dp1 = 0 dp2 = 0 for i in range(len(nums)): dp1, dp2 = dp2, max(dp2, nums[i] + dp1) return dp2
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): dp = [(0) for i in range(n + 1)] data = a.copy() if n >= 1: dp[0] = data[0] if n >= 2: dp[1] = max(data[0], data[1]) if n > 2: for i in range(2, n): cur = data[i] a = dp[i - 1] b = dp[i - 2] + cur dp[i] = max(a, b) return dp[n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, arr, n): if n == 1: return arr[-1] elif n == 2: return max(arr[-1], arr[-2]) dp = [0] * n dp[0] = arr[0] dp[1] = arr[1] dp[2] = dp[0] + arr[2] for i in range(3, n): dp[i] = max(dp[i - 2], dp[i - 3]) + arr[i] return max(dp[-1], dp[-2])
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. Each house has a[i]amount of money present in it. Example 1: Input: n = 6 a[] = {5,5,10,100,10,5} Output: 110 Explanation: 5+100+5=110 Example 2: Input: n = 3 a[] = {1,2,3} Output: 4 Explanation: 1+3=4 Your Task: Complete the functionFindMaxSum()which takes an array arr[] and n as input which returns the maximum money he can get following the rules Expected Time Complexity:O(N). Expected Space Complexity:O(N). Constraints: 1 ≤ n ≤ 10^{4} 1 ≤ a[i] ≤ 10^{4}
class Solution: def FindMaxSum(self, a, n): if n == 0: return 0 high1 = a[0] if n == 1: return high1 high2 = max(a[0], a[1]) if n == 2: return high2 for i in range(2, n): max_val = max(high1 + a[i], high2) high1 = high2 high2 = max_val return max_val
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): k = 2 profit = [] n = len(prices) if n < 2: return 0 for i in range(k + 1): profit.append([]) for j in range(n): profit[i].append(0) for i in range(1, k + 1): tmpMax = profit[i - 1][0] - prices[0] for j in range(n): profit[i][j] = max(profit[i][j - 1], prices[j] + tmpMax) tmpMax = max(tmpMax, profit[i - 1][j] - prices[j]) return profit[k][n - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR BIN_OP VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices, k=2): if not prices: return 0 s1 = s2 = 0 b1 = b2 = -1000000000 for p in prices: b1 = max(b1, -p) s1 = max(s1, b1 + p) b2 = max(b2, s1 - p) s2 = max(s2, b2 + p) return s2
CLASS_DEF FUNC_DEF NUMBER IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): if not prices: return 0 sell_date = [0] * len(prices) min_price = prices[0] for i in range(1, len(prices)): sell_date[i] = max(sell_date[i - 1], prices[i] - min_price) min_price = min(prices[i], min_price) max_price, profit = prices[-1], 0 for i in range(len(prices) - 1, 0, -1): profit = max(profit, max_price - prices[i] + sell_date[i]) max_price = max(max_price, prices[i]) return profit
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): if len(prices) < 2: return 0 buy = [] sell = [] for i in range(2): buy.append(-float("inf")) sell.append(-float("inf")) for i in prices: for j in range(2): if j == 0: buy[j] = max(buy[j], -i) sell[j] = max(sell[j], i + buy[j]) else: buy[j] = max(buy[j], sell[j - 1] - i) sell[j] = max(sell[j], i + buy[j]) return sell[-1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): if not prices: return 0 n = len(prices) sell1 = 0 sell2 = 0 hold1 = -prices[0] hold2 = -prices[0] for i in range(1, n): sell2 = max(prices[i] + hold2, sell2) hold2 = max(sell1 - prices[i], hold2) sell1 = max(prices[i] + hold1, sell1) hold1 = max(-prices[i], hold1) return sell2
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): tmax_profit = 0 rmax_profits = [0] * len(prices) rmax = -1 for ii in range(len(prices) - 2, -1, -1): if prices[rmax] - prices[ii] > rmax_profits[ii + 1]: rmax_profits[ii] = prices[rmax] - prices[ii] else: rmax_profits[ii] = rmax_profits[ii + 1] if prices[ii] > prices[rmax]: rmax = ii lmin = 0 lmax_profit = 0 for ii in range(1, len(prices)): profit = prices[ii] - prices[lmin] if profit > lmax_profit: lmax_profit = profit if prices[ii] < prices[lmin]: lmin = ii tprofit = lmax_profit if ii < len(prices) - 1: tprofit += rmax_profits[ii + 1] if tprofit > tmax_profit: tmax_profit = tprofit return tmax_profit if tmax_profit > 0 else 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): if not prices: return 0 times, n = 2, len(prices) dp = [[(0) for j in range(n)] for i in range(times)] buy_in = -prices[0] for i in range(1, n): dp[0][i] = max(dp[0][i - 1], prices[i] + buy_in) buy_in = max(buy_in, -prices[i]) for k in range(1, times): buy_in = -prices[0] for j in range(1, n): dp[k][j] = max(buy_in + prices[j], dp[k][j - 1]) buy_in = max(buy_in, dp[k - 1][j] - prices[j]) return dp[-1][-1]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): left2right = [0] * len(prices) right2left = [0] * len(prices) dp = prices[:] max_profit = 0 for i in range(1, len(prices)): dp[i] = min(dp[i - 1], prices[i]) left2right[i] = max(left2right[i], prices[i] - dp[i]) max_profit = max(max_profit, prices[i] - dp[i]) dp = prices[:] for i in reversed(list(range(len(prices) - 1))): dp[i] = max(dp[i + 1], prices[i]) right2left[i] = max(right2left[i + 1], dp[i] - prices[i]) print(right2left) for i in range(len(prices) - 1): max_profit = max(max_profit, left2right[i] + right2left[i + 1]) return max_profit
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1: Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.   Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3. Example 2: Input: [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.   Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are   engaging multiple transactions at the same time. You must sell before buying again. Example 3: Input: [7,6,4,3,1] Output: 0 Explanation: In this case, no transaction is done, i.e. max profit = 0.
class Solution: def maxProfit(self, prices): length = len(prices) if length == 0: return 0 f1 = [(0) for i in range(length)] f2 = [(0) for i in range(length)] minV = prices[0] f1[0] = 0 for i in range(1, length): minV = min(minV, prices[i]) f1[i] = max(f1[i - 1], prices[i] - minV) maxV = prices[length - 1] f2[length - 1] = 0 for i in range(length - 2, -1, -1): maxV = max(maxV, prices[i]) f2[i] = max(f2[i + 1], maxV - prices[i]) res = 0 for i in range(length): if f1[i] + f2[i] > res: res = f1[i] + f2[i] return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, n, m, arr, graph): adj = [[(0) for i in range(n + 1)] for i in range(n + 1)] dp = [[(-1) for i in range(1 << n)] for i in range(n + 1)] for i in range(len(graph)): adj[graph[i][0]][graph[i][1]] = 1 adj[graph[i][1]][graph[i][0]] = 1 def solve(mask, prev): if mask == 0: return 1 if prev != -1 and dp[prev][mask] != -1: return dp[prev][mask] res = 0 for i in range(n): if mask & 1 << i > 0: if prev == -1 or adj[prev][arr[i]]: res += solve(mask ^ 1 << i, arr[i]) dp[prev][mask] = res return res return solve((1 << n) - 1, -1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): adj_matrix = [([False] * N) for i in range(N)] for i in range(M): u = graph[i][0] - 1 v = graph[i][1] - 1 adj_matrix[u][v] = True adj_matrix[v][u] = True total = pow(2, N) dp = [([0] * total) for i in range(N)] for i in range(N): dp[i][1 << N - 1 - i] = 1 for mask in range(total): for i in range(N): i_selected = 1 << N - 1 - i if mask & i_selected: rem_mask = mask ^ i_selected if rem_mask > 0: for j in range(N): j_selected = 1 << N - 1 - j if ( rem_mask & j_selected and adj_matrix[arr[i] - 1][arr[j] - 1] ): dp[i][mask] += dp[j][rem_mask] ans = 0 for i in range(N): ans += dp[i][total - 1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): arr.sort() adjacency_list = self.construct_graph(graph, N) return self.generate_permutation(-1, arr, adjacency_list, 0, N, {}) def construct_graph(self, graph, N): adjacency_list = [([False] * (N + 1)) for __ in range(N + 1)] for arr in graph: adjacency_list[arr[0]][arr[1]] = True adjacency_list[arr[1]][arr[0]] = True return adjacency_list def generate_permutation(self, prev, arr, graph, state, N, dp): if N == 0: return 1 res = 0 cur_dp = dp.get(state, {}) for indx, num in enumerate(arr): cur_indx = 1 << indx if cur_indx & state == 0 and (prev == -1 or graph[num][prev]): if cur_dp.get(num, -1) != -1: res += cur_dp[num] continue temp = self.generate_permutation( num, arr, graph, state | cur_indx, N - 1, dp ) res += temp cur_dp[num] = temp dp[state] = cur_dp return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR DICT FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def dfs(self, cur, mask, g, dp, total, N, arr): if mask == total: return 1 if dp[cur][mask] != -1: return dp[cur][mask] temp = 0 for i in range(N): if mask & 1 << i > 0 or g[arr[cur] - 1][arr[i] - 1] != 1: continue temp += self.dfs(i, mask | 1 << i, g, dp, total, N, arr) dp[cur][mask] = temp return dp[cur][mask] def luckyPermutations(self, N, M, arr, graph): ans = 0 total = 2**N - 1 temp = set() for i in arr: temp.add(i) dp = [[(-1) for i in range(2**N)] for j in range(N)] g = [[(-1) for i in range(N)] for j in range(N)] for x in graph: g[x[0] - 1][x[1] - 1] = 1 g[x[1] - 1][x[0] - 1] = 1 ans = 0 for i in range(N): ans += self.dfs(i, 1 << i, g, dp, total, N, arr) return ans
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
import sys sys.setrecursionlimit(10**8) class Solution: def luckyPermutations(self, N, M, arr, graph): adjmatrix = [[(0) for i in range(N)] for j in range(N)] for x in graph: node1 = x[0] - 1 node2 = x[1] - 1 adjmatrix[node1][node2] = 1 adjmatrix[node2][node1] = 1 dp = [[(-1) for i in range(1 << N)] for j in range(16)] mask = (1 << N) - 1 return self.helper(mask, arr, dp, -1, adjmatrix) def helper(self, mask, arr, dp, prev, adjmatrix): if mask == 0: return 1 if prev != -1 and dp[prev][mask] != -1: return dp[prev][mask] count = 0 for i in range(len(arr)): if mask & 1 << i and ( prev == -1 or self.checkedge(arr[i], prev, adjmatrix) ): mask ^= 1 << i count += self.helper(mask, arr, dp, arr[i], adjmatrix) mask ^= 1 << i dp[prev][mask] = count return count def checkedge(self, val1, val2, adjmatrix): if adjmatrix[val1 - 1][val2 - 1] and adjmatrix[val2 - 1][val1 - 1]: return True return False
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def genKey(self, v1, v2): return str(v1) + "," + str(v2) def helper(self, mask, arr, prev, adj, dp): key = self.genKey(mask, prev) if key in dp: return dp[key] if mask == 0: return 1 cnt = 0 for i in range(len(arr)): bitset = 1 << i if mask & bitset: curr = arr[i] if prev == -1 or adj[curr][prev]: cnt += self.helper(mask ^ bitset, arr, curr, adj, dp) dp[key] = cnt return cnt def luckyPermutations(self, N, M, arr, graph): adj = [[(False) for _ in range(16)] for _ in range(16)] for i in range(M): adj[graph[i][0]][graph[i][1]] = True adj[graph[i][1]][graph[i][0]] = True mask = (1 << N) - 1 dp = {} return self.helper(mask, arr, -1, adj, dp)
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): ans = 0 d = {} for item in arr: if item in d: d[item] += 1 else: d[item] = 1 grph = [[] for _ in range(N)] for a, b in graph: grph[a - 1].append(b - 1) grph[b - 1].append(a - 1) dp = [[(-1) for _ in range(1 << N)] for __ in range(N)] visited = [(0) for _ in range(N)] for i in d: ans += self.get(grph, i - 1, N - 1, visited, d, dp) return ans def get(self, graph, node, N, visited, d, dp): b = 0 bit = 1 for i in d: for j in range(visited[i - 1]): b |= bit bit <<= 1 bit <<= d[i] - visited[i - 1] if node + 1 not in d: return 0 elif N == 0: return 1 elif dp[node][b] != -1: return dp[node][b] else: visited[node] += 1 ans = 0 for item in graph[node]: if visited[item] < d.get(item + 1, 0): ans += self.get(graph, item, N - 1, visited, d, dp) * ( d[node + 1] - visited[node] + 1 ) visited[node] -= 1 dp[node][b] = ans return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
import sys sys.setrecursionlimit(1000000) def f(adj, A, mask, prev, dp): if mask == 0: return 1 if dp[prev][mask] != -1: return dp[prev][mask] c = 0 for i in range(len(A)): if mask & 1 << i > 0: if prev == -1 or adj[A[prev]][A[i]]: c += f(adj, A, mask ^ 1 << i, i, dp) dp[prev][mask] = c return c class Solution: def luckyPermutations(self, N, M, A, graph): adj = [[(0) for j in range(N + 1)] for j in range(N + 1)] sz = 1 << N for u, v in graph: adj[u][v] = 1 adj[v][u] = 1 dp = [[(-1) for i in range(sz)] for i in range(N + 1)] ans = f(adj, A, sz - 1, -1, dp) return ans
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): edges = set() for [a, b] in graph: edges.add((a, b)) edges.add((b, a)) dp = dict() ans = 0 for i in range(N): state = (i << 16) + (1 << i) ans += self.backtracking(state, dp, arr, edges) return ans def backtracking(self, state, dp, arr, edges): if state not in dp: i = state >> 16 visited = state - (i << 16) if visited + 1 == 1 << len(arr): ans = 1 else: ans = 0 for j in range(len(arr)): if i == j or (arr[i], arr[j]) not in edges or visited & 1 << j != 0: continue state2 = visited + (j << 16) + (1 << j) ans += self.backtracking(state2, dp, arr, edges) dp[state] = ans return dp[state]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): dp = [[(0) for j in range(1 << N)] for j in range(N)] for i in range(N): dp[i][1 << i] = 1 adj = [[(0) for j in range(N)] for j in range(N)] for edge in graph: adj[edge[0] - 1][edge[1] - 1] = 1 adj[edge[1] - 1][edge[0] - 1] = 1 for bitmask in range(1, 1 << N): for i in range(N): if 1 & bitmask >> i: for j in range(N): if ( j != i and arr[j] != arr[i] and 1 & bitmask >> j and adj[arr[i] - 1][arr[j] - 1] ): dp[i][bitmask] += dp[j][bitmask ^ 1 << i] ans = 0 for i in range(N): ans += dp[i][(1 << N) - 1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def dfs(self, arr, mask, states, N, adj): used = [] unused = [] for i in range(N): if mask & 1 << i: used.append(i) else: unused.append(i) for x in used: states[x][mask] = 0 for y in unused: if adj[arr[x] - 1][arr[y] - 1]: new_mask = mask + (1 << y) if states[y][new_mask] == -1: self.dfs(arr, new_mask, states, N, adj) states[x][mask] += states[y][new_mask] def luckyPermutations(self, N, M, arr, graph): adj = [[(0) for i in range(N)] for j in range(N)] for x, y in graph: adj[x - 1][y - 1] = 1 adj[y - 1][x - 1] = 1 num_masks = 1 << N states = [[(-1) for i in range(num_masks)] for j in range(N)] ans = 0 for i in range(N): states[i][(1 << N) - 1] = 1 for i in range(N): self.dfs(arr, 1 << i, states, N, adj) ans += states[i][1 << i] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN VAR
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
import sys sys.setrecursionlimit(100000000) class Solution: conn = list() dp = list() def rec(self, arr, mask, prev): if prev != -1 and self.dp[mask][prev] != -1: return self.dp[mask][prev] if mask == 0: return 1 count = 0 for i in range(len(arr)): if mask & 1 << i > 0: curr = arr[i] if prev == -1 or self.conn[prev][curr]: count += self.rec(arr, mask ^ 1 << i, curr) self.dp[mask][prev] = count return count def luckyPermutations(self, N, M, arr, graph): self.conn = [([False] * (N + 1)) for _ in range(N + 2)] self.dp = [([-1] * 16) for _ in range((1 << 16) + 1)] mask = (1 << N) - 1 for one_graph in graph: self.conn[one_graph[0]][one_graph[1]] = True self.conn[one_graph[1]][one_graph[0]] = True return self.rec(arr, mask, -1)
IMPORT EXPR FUNC_CALL VAR NUMBER CLASS_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER
You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list. Your task is to find the number of lucky permutations of the given array. An array permutation is said to be lucky if for every node V_{i }[1 <= i <= N-1] in the array there exists an edge between the nodes V_{i }and V_{i+1 }in the given graph. Example 1: Input: N = 3, M = 2 arr = {1, 2, 3} graph = {{3, 1}, {1, 2}} Output: 2 Explanation: All possible permutations of the array are as follows- {1,2,3}: There is an edge between 1 and 2 in the graph but not betwen 2 and 3. {2,1,3}: There is an edge between (2,1) and (1,3) in the graph. {3,1,2}: There is an edge between (3,1) and (1,2) in the graph. Out of the 3 possible permutations, 2 are lucky. Therefore, answer is 2. Example 2: Input: N = 2, M = 1 arr = {1, 1} graph = {{1, 2}} Output : 0 Explanation: There is no lucky permutation in the given graph. Your Task: You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations. Expected Time Complexity: O(N^{2}*2^{N}) Expected Auxiliary Space: O(N*2^{N}) Constraints: 2 ≤ N ≤ 15 1 ≤ M ≤ (N*(N-1))/2 1 ≤ arr[i], graph[i][j] ≤ N There are no self-loops and repeated edges in the graph.
class Solution: def luckyPermutations(self, N, M, arr, graph): mat = [[(0) for i in range(N + 1)] for i in range(N + 1)] for i, j in graph: mat[i][j] = 1 mat[j][i] = 1 dp = [[(0) for i in range(1 << N)] for i in range(N)] for i in range(N): dp[i][1 << i] = 1 for bit in range(1, 1 << N): for i in range(N): if 1 & bit >> i: for j in range(N): if i != j and arr[i] != arr[j] and 1 & bit >> j: if mat[arr[i]][arr[j]]: dp[i][bit] += dp[j][bit ^ 1 << i] ans = 0 for i in dp: ans += i[-1] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER 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: dic = collections.defaultdict(int) sums = {(-1): 0} total = sum(stoneValue) for i, s in enumerate(stoneValue): sums[i] = sums[i - 1] + s for i in sums: sums[i] = total - sums[i] + stoneValue[i] sums.pop(-1) def dfs(s=0): if s not in dic and s < len(stoneValue): dic[s] = sums[s] - min([dfs(s + i + 1) for i in range(3)]) return dic[s] dfs() return "Bob" if total > 2 * dic[0] else "Alice" if total < 2 * dic[0] else "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER RETURN VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP NUMBER VAR NUMBER STRING VAR BIN_OP 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: length = len(stoneValue) dp = {} kA = "Alice" kB = "Bob" for index in range(length - 1, -1, -1): minus = length - index rA = rB = 0 if minus >= 3: c1 = stoneValue[index] + stoneValue[index + 1] + stoneValue[index + 2] c2 = stoneValue[index] + stoneValue[index + 1] c3 = stoneValue[index] rA = max( c1 + dp.get((index + 3, kB), 0), c2 + dp.get((index + 2, kB), 0), c3 + dp.get((index + 1, kB), 0), ) rB = min( -c1 + dp.get((index + 3, kA), 0), -c2 + dp.get((index + 2, kA), 0), -c3 + dp.get((index + 1, kA), 0), ) elif minus == 2: c1 = stoneValue[index] + stoneValue[index + 1] c2 = stoneValue[index] rA = max(c1, c2 + dp.get((index + 1, kB), 0)) rB = min(-c1, -c2 + dp.get((index + 1, kA), 0)) elif minus == 1: c1 = stoneValue[index] rA = c1 rB = -c1 dp[index, kA] = rA dp[index, kB] = rB result = dp.get((0, kA)) if result > 0: return "Alice" elif result < 0: return "Bob" else: return "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER 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 BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER 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: A = stoneValue dp = [0] * 3 for i in range(len(A) - 1, -1, -1): dp[i % 3] = max(sum(A[i : i + k]) - dp[(i + k) % 3] for k in (1, 2, 3)) if dp[0] > 0: return "Alice" elif dp[0] < 0: return "Bob" else: return "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER 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 NUMBER 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: stone_sum = 0 dp = [0] * (len(stoneValue) + 1) + [float("inf")] * 2 for i in range(len(stoneValue) - 1, -1, -1): stone_sum += stoneValue[i] chose = min(dp[i + 1], dp[i + 2], dp[i + 3]) dp[i] = stone_sum - chose if dp[i] > stone_sum / 2: return "Alice" elif dp[i] < stone_sum / 2: return "Bob" else: return "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER RETURN STRING IF VAR 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: ts = sum(stoneValue) n = len(stoneValue) a = [(0) for i in range(n)] for i in range(n - 1, -1, -1): if i == n - 1: a[i] = stoneValue[i] else: a[i] = a[i + 1] + stoneValue[i] cache = {} def process(i): if i == n: return 0 else: if not i in cache: ans = -float("inf") if i < n: ans = max( ans, stoneValue[i] + (a[i + 1] - process(i + 1) if i + 1 < n else 0), ) if i + 1 < n: ans = max( ans, stoneValue[i] + stoneValue[i + 1] + (a[i + 2] - process(i + 2) if i + 2 < n else 0), ) if i + 2 < n: ans = max( ans, stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] + (a[i + 3] - process(i + 3) if i + 3 < n else 0), ) cache[i] = ans return cache[i] Alice = process(i) Bob = ts - Alice if Alice > Bob: return "Alice" elif Bob > Alice: return "Bob" else: return "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER 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 ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER 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 BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR 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: numStones = len(stoneValue) dp = [-float("inf")] * numStones for i in range(numStones - 1, -1, -1): localTotal = 0 for j in range(i, min(i + 3, numStones)): localTotal += stoneValue[j] otherPlayerMax = dp[j + 1] if j + 1 < numStones else 0 dp[i] = max(dp[i], localTotal - otherPlayerMax) winner = "Alice" if dp[0] > 0 else "Bob" if dp[0] < 0 else "Tie" return winner
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING 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 BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER STRING VAR NUMBER NUMBER STRING STRING 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: dp = {} total = sum(stoneValue) def help(start, s): if start in dp: return dp[start] if start >= len(stoneValue): return 0 dp[start] = max( s - help(start + 1, s - stoneValue[start]), s - help(start + 2, s - sum(stoneValue[start : start + 2])), s - help(start + 3, s - sum(stoneValue[start : start + 3])), ) return dp[start] A = help(0, total) if 2 * A > total: return "Alice" elif 2 * A == total: return "Tie" else: return "Bob"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP NUMBER VAR VAR RETURN STRING IF BIN_OP NUMBER 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, sv: List[int]) -> str: n = len(sv) sv += [0, 0] dp = [(0) for _ in range(n + 3)] for i in range(n - 1, -1, -1): dp[i] = max( sv[i] - dp[i + 1], sv[i] + sv[i + 1] - dp[i + 2], sv[i] + sv[i + 1] + sv[i + 2] - dp[i + 3], ) 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 VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP 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
def answer(x, y, alice, bob): if alice[x] > bob[y]: return "Alice" elif alice[x] < bob[y]: return "Bob" else: return "Tie" class Solution: def stoneGameIII(self, stoneValue: List[int]) -> str: if len(stoneValue) == 1: if stoneValue[0] > 0: return "Alice" elif stoneValue[0] == 0: return "Tie" else: return "Bob" alice = [0] * len(stoneValue) bob = [0] * len(stoneValue) alice[-1] = stoneValue[-1] bob[-1] = stoneValue[-1] cumSumm = [0] * len(stoneValue) summ = stoneValue[-1] cumSumm[-1] = summ for i in range(len(stoneValue) - 2, -1, -1): summ += stoneValue[i] cumSumm[i] = summ if i + 2 == len(stoneValue): alice[i] = max( stoneValue[i] + stoneValue[i + 1], stoneValue[i] + cumSumm[i + 1] - bob[i + 1], ) bob[i] = max( stoneValue[i] + stoneValue[i + 1], stoneValue[i] + cumSumm[i + 1] - alice[i + 1], ) continue if i + 3 == len(stoneValue): alice[i] = max( stoneValue[i] + cumSumm[i + 1] - bob[i + 1], stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - bob[i + 2], stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2], ) bob[i] = max( stoneValue[i] + cumSumm[i + 1] - alice[i + 1], stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - alice[i + 2], stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2], ) continue alice[i] = max( stoneValue[i] + cumSumm[i + 1] - bob[i + 1], stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - bob[i + 2], stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] + cumSumm[i + 3] - bob[i + 3], ) bob[i] = max( stoneValue[i] + cumSumm[i + 1] - alice[i + 1], stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - alice[i + 2], stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] + cumSumm[i + 3] - alice[i + 3], ) if len(stoneValue) == 2: i = 0 if alice[0] == stoneValue[i] + cumSumm[i + 1] - bob[i + 1]: x, y = i, i + 1 return answer(x, y, alice, bob) if len(stoneValue) == 3: i = 0 if alice[0] == stoneValue[i] + cumSumm[i + 1] - bob[i + 1]: x, y = i, i + 1 elif ( alice[0] == stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - bob[i + 2] ): x, y = i, i + 2 return answer(x, y, alice, bob) i = 0 if alice[0] == stoneValue[i] + cumSumm[i + 1] - bob[i + 1]: x, y = i, i + 1 elif ( alice[0] == stoneValue[i] + stoneValue[i + 1] + cumSumm[i + 2] - bob[i + 2] ): x, y = i, i + 2 elif ( alice[0] == stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] + cumSumm[i + 3] - bob[i + 3] ): x, y = i, i + 3 return answer(x, y, alice, bob)
FUNC_DEF IF VAR VAR VAR VAR RETURN STRING IF VAR VAR VAR VAR RETURN STRING RETURN STRING CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER 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 BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER 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 BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR 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, stoneValue: List[int]) -> str: self.cache = {} score = self.solve(0, stoneValue) return "Alice" if score > 0 else "Bob" if score < 0 else "Tie" def solve(self, i, stoneValue): n = len(stoneValue) if i >= n: return 0 if i in self.cache: return self.cache[i] res = float("-inf") presum = 0 for x in range(1, 4): if i + x - 1 >= n: break presum += stoneValue[i + x - 1] res = max(res, presum - self.solve(i + x, stoneValue)) self.cache[i] = res return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER VAR RETURN VAR NUMBER STRING VAR NUMBER STRING STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR 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 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 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, nums: List[int]) -> str: n = len(nums) total = sum(nums) presum = [nums[-1]] if n > 1: presum.insert(0, nums[-2] + presum[0]) nums[-2] = max(nums[-2], presum[0]) if n > 2: presum.insert(0, nums[-3] + presum[0]) nums[-3] = max( [ nums[-3] + presum[1] - nums[-2], nums[-3] + presum[1] - presum[2], presum[0], ] ) for i in range(n - 4, -1, -1): get_one = nums[i] + presum[0] - nums[i + 1] get_two = nums[i] + presum[0] - presum[1] + presum[1] - nums[i + 2] get_three = nums[i] + presum[0] - presum[2] + presum[2] - nums[i + 3] presum.pop() presum.insert(0, presum[0] + nums[i]) nums[i] = max([get_one, get_two, get_three]) if nums[0] == presum[0] / 2: return "Tie" elif nums[0] > presum[0] / 2: return "Alice" return "Bob"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER BIN_OP 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 bottomUpDP(self, stoneValue: List[int]) -> str: n = len(stoneValue) suffix_sum = [(0) for _ in range(n + 1)] dp = [(0) for _ in range(n + 1)] for i in range(n - 1, -1, -1): suffix_sum[i] = stoneValue[i] + suffix_sum[i + 1] for i in range(n - 1, -1, -1): player_pick_two_stones, player_pick_three_stones = float("-inf"), float( "-inf" ) player_pick_one_stone = stoneValue[i] + suffix_sum[i + 1] - dp[i + 1] if i + 1 < n: player_pick_two_stones = ( stoneValue[i] + stoneValue[i + 1] + suffix_sum[i + 2] - dp[i + 2] ) if i + 2 < n: player_pick_three_stones = ( stoneValue[i] + stoneValue[i + 1] + stoneValue[i + 2] + suffix_sum[i + 3] - dp[i + 3] ) dp[i] = max( player_pick_one_stone, player_pick_two_stones, player_pick_three_stones ) if 2 * dp[0] == suffix_sum[0]: return "Tie" elif 2 * dp[0] > suffix_sum[0]: return "Alice" else: return "Bob" 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 VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN 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 BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN STRING IF BIN_OP NUMBER VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING VAR 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 = [[-math.inf, -math.inf] for _ in range(n + 1)] dp[-1] = [0, 0] for i in range(n - 1, -1, -1): for j in range(i + 1, min(i + 4, n + 1)): if dp[i][0] < dp[j][1] + sum(stoneValue[i:j]): dp[i][0] = dp[j][1] + sum(stoneValue[i:j]) dp[i][1] = dp[j][0] alice, bob = dp[i] 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 ASSIGN VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR 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: l = len(stoneValue) dp = [0] * (l + 1) for i in range(l - 1, -1, -1): val = stoneValue[i] dp[i] = val - dp[i + 1] if i + 1 < l: val += stoneValue[i + 1] dp[i] = max(dp[i], val - dp[i + 2]) if i + 2 < l: val += stoneValue[i + 2] dp[i] = max(dp[i], val - dp[i + 3]) if dp[0] > 0: return "Alice" elif dp[0] == 0: 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP 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, arr: List[int]) -> str: ln, dp = len(arr), [float("-inf") for _ in range(len(arr))] dp.append(0) for i in range(ln - 1, -1, -1): sm = 0 for j in range(i, min(ln, i + 3)): sm += arr[j] dp[i] = max(dp[i], sm - dp[j + 1]) if dp[0] > 0: return "Alice" elif dp[0] < 0: return "Bob" return "Tie"
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING RETURN STRING VAR