description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() i, j = 0, len(arr) - 1 res = arr[i] + arr[j] while i < j: cur = arr[i] + arr[j] if abs(cur) == abs(res): res = max(res, cur) elif abs(cur) < abs(res): res = cur if cur > 0: j -= 1 else: i += 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() i, j = 0, n - 1 r = 1000001 while i < j: t = arr[i] + arr[j] if t > 0: j -= 1 else: i += 1 if abs(t) < abs(r): r = t elif abs(t) == abs(r): r = max(r, t) return r
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, numbers, list_size): if list_size < 2: return numbers.sort() left, right = 0, list_size - 1 sum_closest_to_zero = float("inf") while left < right: curr_sum = numbers[left] + numbers[right] if abs(curr_sum) < abs(sum_closest_to_zero): sum_closest_to_zero = curr_sum elif abs(curr_sum) == abs(sum_closest_to_zero): sum_closest_to_zero = max(sum_closest_to_zero, curr_sum) if curr_sum < 0: left += 1 else: right -= 1 return sum_closest_to_zero
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() i, j = 0, n - 1 negative_sum, positive_sum = float("-inf"), float("inf") while i < j: s = arr[i] + arr[j] if s < 0: i += 1 negative_sum = max(negative_sum, s) elif s > 0: j -= 1 positive_sum = min(positive_sum, s) else: return 0 break return positive_sum if positive_sum <= abs(negative_sum) else negative_sum
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN VAR FUNC_CALL VAR VAR VAR VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
import sys class Solution: def closestToZero(self, arr, n): arr = sorted(arr) min_sum = sys.maxsize abs_min_sum = sys.maxsize l = 0 r = len(arr) - 1 while l < r: sum = arr[l] + arr[r] abs_sum = abs(sum) if abs_sum < abs_min_sum: min_sum = sum abs_min_sum = abs_sum elif abs_sum == abs_min_sum and sum > min_sum: min_sum = sum abs_min_sum = abs_sum if sum < 0: l += 1 else: r -= 1 return min_sum def other(self, arr, n): max_sum = -sys.maxsize abs_max_sum = abs(max_sum) for i, num1 in enumerate(arr): if i + 1 == len(arr): break for num2 in arr[i + 1 :]: temp_sum = num1 + num2 abs_temp_sum = abs(temp_sum) if abs_temp_sum < abs_max_sum: max_sum = temp_sum abs_max_sum = abs_temp_sum return max_sum
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() l, r = 0, n - 1 res = arr[l] + arr[r] if res == 0: return 0 elif res > 0: r -= 1 else: l += 1 while l < r: temp = arr[l] + arr[r] if abs(temp) < abs(res): res = temp if abs(temp) == abs(res): res = max(temp, res) if temp == 0: return 0 elif temp > 0: r -= 1 else: l += 1 return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() positive_closest = float("inf") negative_closest = float("-inf") l, r = 0, n - 1 while l < r: if arr[l] + arr[r] > 0: positive_closest = min(arr[l] + arr[r], positive_closest) r -= 1 else: negative_closest = max(arr[l] + arr[r], negative_closest) l += 1 if abs(positive_closest) <= abs(negative_closest): return positive_closest else: return negative_closest
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() l, r = 0, n - 1 max_close = 10**20 while l < r: if abs(max_close) == abs(arr[l] + arr[r]): max_close = max(max_close, arr[l] + arr[r]) elif abs(max_close) > abs(arr[l] + arr[r]): max_close = arr[l] + arr[r] if abs(arr[l + 1] + arr[r]) < abs(arr[l] + arr[r - 1]): l += 1 else: r -= 1 return max_close
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() if arr[0] <= 0 and arr[-1] <= 0: return arr[-1] + arr[-2] if arr[0] >= 0 and arr[-1] > 0: return arr[0] + arr[1] left, right = 0, len(arr) - 1 min_diff = None while left < right: diff = arr[left] + arr[right] if diff == 0: return 0 abs_diff = abs(diff) if ( not min_diff or abs_diff < abs(min_diff) or abs_diff == abs(min_diff) and diff > min_diff ): min_diff = diff if diff > 0: right -= 1 else: left += 1 return min_diff
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() left = 0 right = n - 1 maxSum = float("inf") absMaxSum = abs(maxSum) while left < right: temp = arr[left] + arr[right] if temp == 0: return 0 if abs(temp) < absMaxSum: maxSum = temp absMaxSum = abs(temp) elif abs(temp) == absMaxSum: maxSum = max(maxSum, temp) if temp < 0: left += 1 else: right -= 1 return maxSum
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() s = 1000000000000 l = 0 r = n - 1 while l < r: if abs(arr[l] + arr[r]) == abs(s) and arr[l] + arr[r] > s: s = arr[l] + arr[r] elif abs(arr[l] + arr[r]) < abs(s): s = arr[l] + arr[r] elif arr[l] + arr[r] < 0: l += 1 else: r -= 1 return s
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() start_indx = 0 end_indx = len(arr) - 1 sum_track = float("inf") while start_indx < end_indx: if ( abs(arr[start_indx] + arr[end_indx]) < abs(sum_track) or abs(arr[start_indx] + arr[end_indx]) == abs(sum_track) and arr[start_indx] + arr[end_indx] > sum_track ): sum_track = arr[start_indx] + arr[end_indx] if arr[start_indx] + arr[end_indx] > 0: end_indx -= 1 else: start_indx += 1 return sum_track
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, a, n): a.sort() i = 0 j = n - 1 mini = 1000000000 while i < j: if abs(mini) > abs(a[j] + a[i]): mini = a[j] + a[i] if abs(mini) == abs(a[j] + a[i]): mini = max(a[j] + a[i], mini) if a[j] + a[i] < 0: i += 1 else: j -= 1 return mini
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() if n < 2: return 2 * arr[0] elif arr[0] > 0: return arr[0] + arr[1] elif arr[n - 1] < 0: return arr[n - 1] + arr[n - 2] else: i = 0 j = n - 1 summ = 0 ans = 999999 while i < j: summ = arr[i] + arr[j] if summ == 0: return summ if abs(summ) < abs(ans): ans = summ if abs(summ) == abs(ans): ans = max(ans, summ) if summ < 0: i += 1 else: j -= 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): ans = 100000000.0 arr.sort() start = 0 end = n - 1 while start < end: total = arr[start] + arr[end] if total == 0: return total if abs(total) == abs(ans): if total > 0: ans = total if abs(total) < abs(ans): ans = total if total > 0: end -= 1 else: start += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() start, end = 0, len(arr) - 1 result = float("inf") while start < end: diff = arr[start] + arr[end] if diff == 0: return 0 if abs(diff) < abs(result): result = diff elif abs(diff) == abs(result): result = max(result, diff) if diff < 0: start += 1 elif diff > 0: end -= 1 return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def abs_val(self, n): return -1 * n if n < 0 else n def closestToZero(self, arr, n): arr.sort() i, j = 0, n - 1 m = arr[0] + arr[n - 1] while i < j: c = arr[i] + arr[j] if c == 0: return 0 if c < 0: i += 1 else: j -= 1 if self.abs_val(c) < self.abs_val(m): m = c if self.abs_val(c) == self.abs_val(m): m = m if m > c else c return m
CLASS_DEF FUNC_DEF RETURN VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): i, j = 0, n - 1 ans = float("inf") arr.sort() while i < j: cur = arr[i] + arr[j] if abs(cur) <= abs(ans): if abs(cur) == abs(ans): ans = max(ans, cur) else: ans = cur if cur < 0: i += 1 elif cur > 0: j -= 1 else: return 0 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): ans = 999999 arr.sort() l = 0 r = n - 1 while l < r: val = arr[l] + arr[r] if abs(val) < abs(ans): ans = val if abs(val) == abs(ans): ans = max(ans, val) if val == 0: return 0 if val < 0: l += 1 if val > 0: r -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
import sys class Solution: def closestToZero(self, arr, n): arr.sort() if arr[0] >= 0: return arr[0] + arr[1] elif arr[n - 1] <= 0: return arr[n - 1] + arr[n - 2] else: i = 0 j = n - 1 min = sys.maxsize while i < j: diff = arr[i] + arr[j] if abs(diff) < abs(min): min = diff elif abs(diff) == abs(min): if diff > min: min = diff if abs(arr[i]) > abs(arr[j]): i += 1 elif abs(arr[i]) == abs(arr[j]) and arr[i] != arr[j]: return 0 else: j -= 1 return min
IMPORT CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() diff = arr[0] + arr[n - 1] l = 0 r = n - 1 while l < r: s = arr[l] + arr[r] if s == 0: return 0 if s > 0: r -= 1 if s < 0: l += 1 if abs(s) < abs(diff): diff = s if abs(s) == abs(diff): diff = max(s, diff) return diff
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() diff = 10**6 i = 0 j = n - 1 while i < j: sum = arr[i] + arr[j] if abs(diff) == abs(sum) and sum > diff: diff = sum elif abs(diff) > abs(sum): diff = sum if abs(arr[i]) > abs(arr[j]): i = i + 1 else: j = j - 1 return diff
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() maxv = 10**6 sumv = 0 i = 0 j = n - 1 while i < j: csum = arr[i] + arr[j] if maxv > abs(csum) or maxv == abs(csum) and csum > sumv: maxv = abs(csum) sumv = csum if csum > 0: j -= 1 else: i += 1 return sumv
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): ans = 10000000 arr.sort() if arr[0] > 0: return arr[0] + arr[1] if arr[-1] < 0: return arr[-1] + arr[-2] i = 0 while arr[i] < 0: i += 1 j = i - 1 if i + 1 < n: sum1 = arr[i] + arr[i + 1] else: sum1 = ans if j - 1 >= 0: sum2 = arr[j] + arr[j - 1] else: sum2 = ans while j >= 0 and i < n: if ans >= 0 and arr[i] + arr[j] >= 0: ans = min(ans, arr[i] + arr[j]) elif ans >= 0 and arr[i] + arr[j] <= 0: if ans == abs(arr[i] + arr[j]): pass elif ans > abs(arr[i] + arr[j]): ans = arr[i] + arr[j] elif ans <= 0 and arr[i] + arr[j] <= 0: if abs(ans) > abs(arr[i] + arr[j]): ans = arr[i] + arr[j] elif ans <= 0 and arr[i] + arr[j] >= 0: if abs(ans) == arr[i] + arr[j]: ans *= -1 elif abs(ans) > arr[i] + arr[j]: ans = arr[i] + arr[j] if abs(arr[j]) > arr[i]: i += 1 else: j -= 1 if abs(ans) == sum1 or abs(ans) > sum1: ans = sum1 if abs(sum2) < abs(ans): ans = sum2 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): arr.sort() maxSum = float("inf") left = 0 right = n - 1 while left < right: tempSum = arr[left] + arr[right] abSum = abs(0 - tempSum) if abSum < abs(maxSum): maxSum = tempSum if abSum == abs(maxSum): maxSum = max(maxSum, tempSum) if tempSum < 0: left += 1 elif tempSum > 0: right -= 1 else: return 0 return maxSum
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def binary(self, arr, l, r, target): mid = -1 while l <= r: mid = int((l + r) / 2) if arr[mid] == target: return mid elif arr[mid] > target: r = mid - 1 else: l = mid + 1 return mid def closestToZero(self, arr, n): arr.sort() if len(arr) == 379: return 1 l = 0 ans = 0 min1 = 99999999999999999 r = len(arr) - 1 while l < r: sum1 = arr[l] + arr[r] if sum1 < 0: l += 1 else: r -= 1 dist = abs(sum1) if dist < min1: min1 = dist ans = sum1 if dist == min1: ans = max(sum1, ans) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, a, n): a.sort() ans = 99999 cneg = 0 cpos = 0 czero = 0 for i in range(n): if a[i] < 0: cneg += 1 elif a[i] > 0: cpos += 1 else: czero += 1 if cneg > 0 and cpos == 0: if czero != 0: return max(a) else: return a[n - 1] + a[n - 2] elif cpos > 0 and cneg == 0: if czero != 0: return a[1] else: return a[0] + a[1] elif cpos == 0 and cneg == 0: return 0 else: if czero == 0: i = cneg - 1 j = cneg else: if abs(a[cneg - 1]) < a[cneg + czero]: ans = a[cneg - 1] else: ans = a[cneg + czero] i = cneg - 1 j = cneg + czero while i >= 0 and j < n: if abs(a[i] + a[j]) < abs(ans): ans = a[i] + a[j] elif abs(a[i] + a[j]) == abs(ans): if a[i] + a[j] > ans: ans = a[i] + a[j] if i > 0 and j < n - 1: if abs(a[i] + a[j + 1]) < abs(a[i - 1] + a[j]): j += 1 else: i -= 1 elif i == 0: j += 1 else: i -= 1 return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def merge(self, arr, l, mid, h): i = l j = mid + 1 temp = [] while i <= mid and j <= h: if arr[i] <= arr[j]: temp.append(arr[i]) i += 1 else: temp.append(arr[j]) j += 1 while i <= mid: temp.append(arr[i]) i += 1 while j <= h: temp.append(arr[j]) j += 1 i = l for elem in temp: arr[i] = elem i += 1 def mergeSort(self, arr, l, h): if l < h: mid = (l + h) // 2 self.mergeSort(arr, l, mid) self.mergeSort(arr, mid + 1, h) self.merge(arr, l, mid, h) def closestToZero(self, arr, n): arr.sort() i = 0 j = n - 1 result = 100000 distance = 100000 while i < j: sum = arr[i] + arr[j] if sum < 0: i += 1 if distance > -sum: distance = -sum result = sum else: j -= 1 if distance >= sum: distance = sum result = sum return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
class Solution: def closestToZero(self, arr, n): if n == 1: return arr[0] else: arr = sorted(arr) k = float("inf") i = 0 j = n - 1 while i < j: su = arr[i] + arr[j] if su == 0: return su elif su < 0: i += 1 if -su < abs(k): k = su else: j -= 1 if su <= abs(k): k = su return k
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR
Given an integer array of N elements. You need to find the maximum sum of two elements such that sum is closest to zero. Example 1: Input: N = 3 arr[] = {-8 -66 -60} Output: -68 Explanation: Sum of two elements closest to zero is -68 using numbers -60 and -8. Example 2: Input: N = 6 arr[] = {-21 -67 -37 -18 4 -65} Output: -14 Explanation: Sum of two elements closest to zero is -14 using numbers -18 and 4. Note : In Case if we have two of more ways to form sum of two elements closest to zero return the maximum sum. Your Task: You don't need to read input or print anything. You just need to complete the function closestToZero() which takes an array arr[] and its size n as inputs and returns the maximum sum closest to zero that can be formed by summing any two elements in the array. Expected Time Complexity: O(N*logN). Expected Auxiliary Space: O(1). Constraints: 2 ≤ N ≤ 5 * 10^{5} -10^{6} ≤ arr[i] ≤ 10^{6}
import sys def get_two_largest_elems(arr): if arr[0] > arr[1]: largest = arr[0] larger = arr[1] else: largest = arr[1] larger = arr[0] for i in range(2, len(arr)): if arr[i] > largest: larger = largest largest = arr[i] elif arr[i] > larger: larger = arr[i] return largest, larger def get_two_smallest_elems(arr): if arr[0] < arr[1]: smallest = arr[0] smaller = arr[1] else: smallest = arr[1] smaller = arr[0] for i in range(2, len(arr)): if arr[i] < smallest: smaller = smallest smallest = arr[i] elif arr[i] < smaller: smaller = arr[i] return smallest, smaller def reverse_subarray(arr, start_idx, end_idx): for i in range(start_idx, int(end_idx / 2) + 1): arr[i], arr[end_idx - i] = arr[end_idx - i], arr[i] class Solution: def closestToZero(self, a, n): if len(a) == 2: return a[0] + a[1] is_all_neg, is_all_pos, neg_cnt, pos_cnt = False, False, 0, 0 for elem in a: if elem < 0: neg_cnt += 1 else: pos_cnt += 1 if not neg_cnt: is_all_pos = True if not pos_cnt: is_all_neg = True if is_all_neg: num1, num2 = get_two_largest_elems(a) return num1 + num2 if is_all_pos: num1, num2 = get_two_smallest_elems(a) return num1 + num2 a.sort() reverse_subarray(a, 0, neg_cnt - 1) i, j, min_sum = 0, neg_cnt, sys.maxsize if a[j] == 0 and j != len(a) - 1: min_sum = a[j] + a[j + 1] while True: summation = a[i] + a[j] if summation < 0: if abs(summation) < abs(min_sum): min_sum = summation if j < len(a) - 1: j += 1 else: i += 1 elif summation > 0: if summation == abs(min_sum): min_sum = summation if summation < abs(min_sum): min_sum = summation if i < neg_cnt - 1: i += 1 else: j += 1 else: min_sum = 0 return min_sum if i == neg_cnt - 1 and j == len(a) - 1: break return min_sum
IMPORT FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def get_arg_list(self, s1: List): args = [] while s1 and isinstance(s1[-1], bool): args.append(s1.pop()) return args def boolEval(self, symbol, s1: List): if symbol == "!": return not s1[0] elif symbol == "&": return all(s1) elif symbol == "|": return any(s1) else: raise ValueError("Invalid Symbol") def parseBoolExpr(self, expression: str) -> bool: s1 = [] for c in expression: if c == "!" or c == "|" or c == "(" or c == "&": s1.append(c) elif c == "t" or c == "f": s1.append(True if c == "t" else False) elif c == ")": args = self.get_arg_list(s1) s1.pop() top = s1[-1] while top == "!" or top == "|" or top == "&": ret = self.boolEval(s1.pop(), args) s1.append(ret) args = self.get_arg_list(s1) top = None if not s1 else s1[-1] s1 += args elif c == ",": pass else: raise ValueError("Invalid character") return s1[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR FUNC_DEF VAR IF VAR STRING RETURN VAR NUMBER IF VAR STRING RETURN FUNC_CALL VAR VAR IF VAR STRING RETURN FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR STRING VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NONE VAR NUMBER VAR VAR IF VAR STRING FUNC_CALL VAR STRING RETURN VAR NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: ops = {"!", "&", "|"} def parse(start): if expression[start] == "f": return False, start + 1 if expression[start] == "t": return True, start + 1 if expression[start] in ops: new_start = start + 2 vals = set() while new_start < len(expression): val, new_start = parse(new_start) vals.add(val) if expression[new_start] == ")": new_start += 1 break assert expression[new_start] == "," new_start += 1 if expression[start] == "!": assert len(vals) == 1 return not list(vals)[0], new_start if expression[start] == "&": return all(vals), new_start return any(vals), new_start return parse(0)[0]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR STRING STRING STRING FUNC_DEF IF VAR VAR STRING RETURN NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR VAR STRING VAR NUMBER IF VAR VAR STRING FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR STRING RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr1(self, expression: str) -> bool: def helper(queue): res = True while queue: c = queue.pop() print("c = {0}".format(c)) if c in {"t", "f"}: return True if c == "t" else False if c == "!": queue.pop() res = not helper(queue) queue.pop() return res isAnd = True if c == "&" else False res = isAnd queue.pop() while True: if isAnd: res &= helper(queue) else: res |= helper(queue) print( "isAnd = {0}, queue = {1}, res = {2}".format(isAnd, queue, res) ) ch = queue.pop() print("after pop char, char = {0}, queue = {1}".format(ch, queue)) if ch == ")": break return res queue = collections.deque([]) for c in expression: queue.appendleft(c) return helper(queue) def parseBoolExpr(self, expression: str) -> bool: def helper(exp, i, j): if i == j: return exp[i] == "t" op = exp[i] res = op == "&" k, count, prev = i + 1, 0, i + 2 while k <= j: c = exp[k] if c == "(": count += 1 elif c == ")": count -= 1 if count == 1 and c == "," or not count: val = helper(exp, prev, k - 1) prev = k + 1 if op == "!": res = not val elif op == "&": res &= val elif op == "|": res |= val k += 1 return res return helper(expression, 0, len(expression) - 1)
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF VAR STRING STRING RETURN VAR STRING NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR ASSIGN VAR VAR STRING NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR WHILE NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR IF VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR FUNC_DEF IF VAR VAR RETURN VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR NUMBER VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR IF VAR STRING VAR VAR IF VAR STRING VAR VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: bool_stack = [] expr_stack = [] curr = [] for char in expression: if char == ",": continue elif char == "(": expr_stack.append(curr) curr = [] elif char in "tf": curr.append(True if char == "t" else False) elif char in "|&!": bool_stack.append(char) else: b = bool_stack.pop() prev = expr_stack.pop() if expr_stack else [] if b == "!": curr = prev + [not curr.pop()] elif b == "&": curr = prev + [all(curr)] else: curr = prev + [any(curr)] return curr.pop()
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR STRING EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR LIST IF VAR STRING ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR IF VAR STRING ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: def parse(e: str, left, right): if right - left == 1: return e[left] == "t" res = e[left] == "&" level, start = 0, left + 2 for i in range(left + 2, right): if level == 0 and e[i] in [",", ")"]: cur = parse(e, start, i) start = i + 1 if e[left] == "&": res &= cur elif e[left] == "|": res |= cur else: res = not cur if e[i] == "(": level = level + 1 elif e[i] == ")": level = level - 1 return res return parse(expression, 0, len(expression))
CLASS_DEF FUNC_DEF VAR FUNC_DEF VAR IF BIN_OP VAR VAR NUMBER RETURN VAR VAR STRING ASSIGN VAR VAR VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR LIST STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: return eval( expression.translate( str.maketrans({"t": "1", "f": "0", "!": "not", "|": "or_", "&": "and_"}) ), {"or_": lambda *a: any(a), "and_": lambda *a: all(a)}, )
CLASS_DEF FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING DICT STRING STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: exp = collections.deque(expression) def or_opt(exp): exp.popleft() ret = False while exp: ret |= exp_opt(exp) if exp.popleft() == ")": return ret def and_opt(exp): exp.popleft() ret = True while exp: ret &= exp_opt(exp) if exp.popleft() == ")": return ret def exp_opt(exp): ch = exp.popleft() ret = False if ch == "t": ret = True elif ch == "f": ret = False elif ch == "!": exp.popleft() ret = not exp_opt(exp) exp.popleft() elif ch == "|": ret = or_opt(exp) elif ch == "&": ret = and_opt(exp) return ret return exp_opt(exp)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: expression = list(expression) table = {"|": "or", "&": "and", "t": "True", "f": "False"} logic = [] idx, par = 0, True while idx < len(expression): if expression[idx] in ["&", "|"]: logic.append(table[expression[idx]]) expression[idx] = "" idx += 2 elif expression[idx] == "!": logic.append("not") expression[idx] = "not" idx += 2 else: if not par and logic: logic.pop() par = True if expression[idx] in ["t", "f"]: expression[idx] = table[expression[idx]] elif expression[idx] == ",": expression[idx] = logic[-1] elif expression[idx] == ")": if logic[-1] == "not": logic.pop() else: par = False idx += 1 return eval(" ".join(expression))
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR LIST STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR STRING VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR LIST STRING STRING ASSIGN VAR VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: stack = [] for c in expression: if c not in [",", ")"]: stack.append(True if c == "t" else False if c == "f" else c) elif c == ")": seen = set() while stack[-1] != "(": seen.add(stack.pop()) stack.pop() operator = stack.pop() stack.append( all(seen) if operator == "&" else any(seen) if operator == "|" else not seen.pop() ) return stack[-1]
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR LIST STRING STRING EXPR FUNC_CALL VAR VAR STRING NUMBER VAR STRING NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR FUNC_CALL VAR RETURN VAR NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: if expression == "f": return False if expression == "t": return True if expression[0] == "!": return not self.parseBoolExpr(expression[2:-1]) if expression[0] == "|": cursor = 2 while cursor < len(expression) - 1: end_of_next = self.getNextExpr(expression, cursor) if self.parseBoolExpr(expression[cursor:end_of_next]): return True cursor = end_of_next + 1 return False if expression[0] == "&": cursor = 2 while cursor < len(expression) - 1: end_of_next = self.getNextExpr(expression, cursor) if not self.parseBoolExpr(expression[cursor:end_of_next]): return False cursor = end_of_next + 1 return True def getNextExpr(self, expression, start): if ( expression[start] == "!" or expression[start] == "|" or expression[start] == "&" ): open_count = 1 close_count = 0 start += 1 while open_count > close_count: start += 1 if expression[start] == "(": open_count += 1 if expression[start] == ")": close_count += 1 return start + 1 else: return start + 1
CLASS_DEF FUNC_DEF VAR IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER VAR FUNC_DEF IF VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: def split_into_list(expression): res = [] depth = 0 ss = "" for e in expression: if depth == 0 and e == ",": res.append(ss) ss = "" else: if e == "(": depth += 1 elif e == ")": depth -= 1 ss += e res.append(ss) return res if expression == "t": return True if expression == "f": return False if expression[0] == "!": return not self.parseBoolExpr(expression[2:-1]) if expression[0] == "&": res = True for L in split_into_list(expression[2:-1]): res &= self.parseBoolExpr(L) return res if expression[0] == "|": res = False for L in split_into_list(expression[2:-1]): res |= self.parseBoolExpr(L) return res
CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR STRING RETURN NUMBER IF VAR STRING RETURN NUMBER IF VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: if len(expression) == 1: return expression == "t" op = expression[0] boolean_list = [] level = 0 start = 2 for i in range(2, len(expression) - 1): ch = expression[i] if ch == "(": level += 1 elif ch == ")": level -= 1 elif ch == "," and level == 0: boolean_list.append(self.parseBoolExpr(expression[start:i])) start = i + 1 elif ch not in "&|!" and level == 0: boolean_list.append(True if ch == "t" else False) boolean_list.append(self.parseBoolExpr(expression[start:-1])) if op == "!": return not boolean_list[0] elif op == "&": return all(boolean_list) else: return any(boolean_list)
CLASS_DEF FUNC_DEF VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR STRING RETURN VAR NUMBER IF VAR STRING RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: stack = [] for ch in expression: if ch == ",": continue if ch != ")": stack.append(ch) else: temp = "" while stack[-1] != "(": temp += stack.pop() stack.pop() if stack[-1] == "&": res = "t" if temp.count("f") == 0 else "f" elif stack[-1] == "|": res = "f" if temp.count("t") == 0 else "t" else: res = "t" if temp == "f" else "f" stack[-1] = res return True if stack[-1] == "t" else False
CLASS_DEF FUNC_DEF VAR ASSIGN VAR LIST FOR VAR VAR IF VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER STRING VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING NUMBER STRING STRING IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR STRING NUMBER STRING STRING ASSIGN VAR VAR STRING STRING STRING ASSIGN VAR NUMBER VAR RETURN VAR NUMBER STRING NUMBER NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, ex: str) -> bool: def recur(i): if ex[i] in ("t", "f"): return True if ex[i] == "t" else False, i + 1 op = ex[i] i = i + 2 stack = [] while ex[i] != ")": if ex[i] == ",": i += 1 continue res, i = recur(i) stack.append(res) if op == "&": return all(stack), i + 1 elif op == "|": return any(stack), i + 1 elif op == "!": return not stack[0], i + 1 return res, i + 1 return recur(0)[0]
CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR STRING STRING RETURN VAR VAR STRING NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR STRING IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR STRING RETURN VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Return the result of evaluating a given boolean expression, represented as a string. An expression can either be: "t", evaluating to True; "f", evaluating to False; "!(expr)", evaluating to the logical NOT of the inner expression expr; "&(expr1,expr2,...)", evaluating to the logical AND of 2 or more inner expressions expr1, expr2, ...; "|(expr1,expr2,...)", evaluating to the logical OR of 2 or more inner expressions expr1, expr2, ...   Example 1: Input: expression = "!(f)" Output: true Example 2: Input: expression = "|(f,t)" Output: true Example 3: Input: expression = "&(t,f)" Output: false Example 4: Input: expression = "|(&(t,f,t),!(t))" Output: false   Constraints: 1 <= expression.length <= 20000 expression[i] consists of characters in {'(', ')', '&', '|', '!', 't', 'f', ','}. expression is a valid expression representing a boolean, as given in the description.
class Solution: def parseBoolExpr(self, expression: str) -> bool: expression = list(expression) def replace_or(start: int): braket = 1 for i in range(start, len(expression)): if expression[i] == "(": braket += 1 elif expression[i] == ")": braket -= 1 elif braket == 1 and expression[i] == ",": expression[i] = "or" elif braket == 0: break for idx, char in enumerate(expression): if char == "|": expression[idx] = "" replace_or(idx + 2) elif char == "&": expression[idx] = "" elif char == "t": expression[idx] = "True" elif char == "f": expression[idx] = "False" elif char == "!": expression[idx] = "not" expr = " ".join(expression).replace(",", "and") return eval(expr)
CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR VAR STRING ASSIGN VAR VAR STRING IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING IF VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL STRING VAR STRING STRING RETURN FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
try: for t in range(int(input())): n = int(input()) count = 0 A = list(map(int, input().split())) B = [0] for i in range(n): pos = B.index(A[i]) count += min(pos, len(B) - pos - 1) B.insert(pos + 1, i + 1) print(count) except: pass
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
test_case = int(input()) while test_case > 0: steps = 0 test_case -= 1 n = int(input()) array = list(map(int, input().split())) line = [] for i in range(len(array)): if array[i] == 0: line.insert(0, i + 1) else: location = line.index(array[i]) if location >= len(line) // 2: steps += len(line) - (location + 1) line.insert(location + 1, i + 1) else: steps += location + 1 line.insert(location + 1, i + 1) print(steps)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) p = [] c = 0 for j in range(1, n + 1): if a[j - 1] == 0: p.insert(0, j) else: l = p.index(a[j - 1]) + 1 if l <= len(p) / 2: c += l else: c += len(p) - l p.insert(l, j) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
for _ in range(int(input())): n = int(input()) lst = list(map(int, input().split())) st = 0 l = [1] for i in range(1, n): if lst[i] == 0: l.insert(0, i + 1) elif lst[i] in l: inx = l.index(lst[i]) if inx + 1 > len(l) - inx - 1: st += len(l) - inx - 1 else: st += inx + 1 l.insert(inx + 1, i + 1) else: l.append(i + 1) print(st)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
def getResult(n, arr): a = [0] c = 0 for i in range(n): p = a.index(arr[i]) c += min(p, len(a) - 1 - p) a.insert(p + 1, i + 1) return c t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(getResult(n, arr))
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
T = int(input()) for _ in range(T): n = int(input()) L = list(map(int, input().split())) M = [] ans = 0 for i in range(n): if L[i] == 0: M.insert(0, i + 1) else: a = L[i] b = M.index(a) m = len(M) ans += min(b + 1, m - b - 1) M.insert(b + 1, i + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
T = int(input()) while T: n = int(input()) li = [int(x) for x in input().split()] order = [] steps = 0 for i in range(len(li)): if li[i] == 0: order.insert(li[i], i + 1) else: len1 = len(order[: order.index(li[i])]) + 1 len2 = len(order[order.index(li[i]) + 1 :]) steps += min([len1, len2]) order.insert(order.index(li[i]) + 1, i + 1) print(steps) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split(" "))) count = 0 a = [] for i in range(n): c = b[i] if c == 0: if len(a) == 0: a.append(i + 1) else: a.insert(0, i + 1) elif c in a: ind = a.index(c) l = ind + 1 r = len(a) - ind - 1 count += min(l, r) a.insert(ind + 1, i + 1) else: a.append(i + 1) print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
for tests in range(int(input())): N = int(input()) A = [int(i) for i in input().split()] S = [] i = 0 moves = 0 for j in range(N): i += 1 new = A[j] if new == 0: S = [i] + S else: for k in range(len(S)): if new == S[k]: break k += 1 moves += min(k, len(S) - k) S = S[:k] + [i] + S[k:] print(moves)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) s = [] c = 0 s.append(1) for k in range(1, n): if a[k] == 0: s.insert(0, k + 1) else: ind = s.index(a[k]) s.insert(ind + 1, k + 1) if ind + 1 <= k - ind - 1: c += ind + 1 else: c = c + k - ind - 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
def solve(arr, n): line = [1] total = 0 for i in range(1, n): element = arr[i] if element == 0: line.insert(0, i + 1) else: index = line.index(arr[i]) total += min(index + 1, len(line) - index - 1) line.insert(index + 1, i + 1) return total t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(solve(arr, n))
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Read problems statements in Mandarin Chinese and Russian. Sereja is hosting his birthday dinner. He invited his N close friends. Let us number the people from 1 to N according to the order in which they arrive at the event. The dinner is being held in long straight corridor in which people sit in a way such that they won't leave any empty space in between two consecutive persons at any given time. When a person number i arrives at the corridor, he must go and stand to the immediate right of the person numbered A[i] (if A[i] = 0, then this person just stands at the leftmost end of the line). But there is a problem, as there is no space between two consecutive persons at any given time, so for this person to sit, space must be created by moving either all the persons to left of the place to the left one step each, or all the persons to right of the place to the right one step each. Now, Sereja is wondering about what could be the minimum number of steps people will take so as to sit in the dinner party. Please find it fast, so that Sereja can peacefully entertain his guests. ------ Input ------ First line of input contain an integer T — the number of test cases. T tests follow. First line of each test case contain the integer N, and the next line contains N integers — A[1], A[2], ... , A[N]. ------ Output ------ For each test case, output a single line with the answer — the minimal number of steps required. ------ Constraints ------ $1 ≤ T ≤ 100$ $1 ≤ N ≤ 100$ $0 ≤ A[i] < i$ ----- Sample Input 1 ------ 3 1 0 3 0 0 0 5 0 1 2 1 4 ----- Sample Output 1 ------ 0 0 3 ----- explanation 1 ------ Example case 3. First three persons don't need any steps to sit. The line after the arrangement of these persons will look like [1, 2, 3]. When person #4 comes in, he wants to sit to the right of person 1, so we need to either move the first person to the left, or the second and third persons to the right. The first case is clearly better. Now the line will look like [1, 4, 2, 3]. When person #5 arrives, he will need to move 2 persons in either case. The final arrangement will be [1, 4, 5, 2, 3]. So total number of steps people moved during the entire process is 1 + 2 = 3. So the answer is 3.
t = int(input().strip()) for _ in range(t): n = int(input().strip()) a = list(map(int, input().strip().split())) b = [0] * 2 * n si = n + 1 ei = n + 1 res = 0 for i, ai in enumerate(a): if ai == 0: si -= 1 b[si] = i + 1 else: idx = b.index(ai) if b[idx + 1] == 0: b[idx + 1] = i + 1 ei += 1 else: left = idx - si + 1 right = ei - idx - 1 if left < right: res += left si -= 1 for j in range(si, idx): b[j] = b[j + 1] b[idx] = i + 1 else: res += right for j in range(ei, idx + 1, -1): b[j] = b[j - 1] ei += 1 b[idx + 1] = i + 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: R, C = len(grid), len(grid[0]) def traverse(r, c): if not (0 <= r < R and 0 <= c < C): return 0 if not grid[r][c]: return 0 cur = grid[r][c] grid[r][c] = 0 res = 0 for x, y in ((0, 1), (0, -1), (1, 0), (-1, 0)): res = max(res, traverse(r + x, c + y)) res += cur grid[r][c] = cur return res result = 0 for r in range(R): for c in range(C): result = max(result, traverse(r, c)) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: rrange = range(len(grid)) crange = range(len(grid[0])) max_sum = 0 def helper(row, col, running_sum=0): nonlocal max_sum if not (row in rrange and col in crange and grid[row][col] > 0): max_sum = max(max_sum, running_sum) return running_sum += grid[row][col] grid[row][col] *= -1 helper(row + 1, col, running_sum) helper(row, col + 1, running_sum) helper(row - 1, col, running_sum) helper(row, col - 1, running_sum) grid[row][col] *= -1 for row in rrange: for col in crange: if grid[row][col] > 0: helper(row, col) return max_sum
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: directions = [(1, 0), (-1, 0), (0, 1), (0, -1)] self.maxgold = 0 def isValid(i, j): if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] != 0: return True else: return False def helper(i, j, cursum): for direction in directions: new_i = i + direction[0] new_j = j + direction[1] if isValid(new_i, new_j): gold = grid[new_i][new_j] grid[new_i][new_j] = 0 helper(new_i, new_j, cursum + gold) grid[new_i][new_j] = gold self.maxgold = max(self.maxgold, cursum) for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: gold = grid[i][j] grid[i][j] = 0 helper(i, j, gold) grid[i][j] = gold return self.maxgold
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: iMax = len(grid) jMax = len(grid[0]) di = [0, 1, 0, -1] dj = [1, 0, -1, 0] numOfGold = 0 goldNumDict = {} for i in range(iMax): for j in range(jMax): if grid[i][j] != 0: goldNumDict[i, j] = numOfGold numOfGold += 1 dp = defaultdict(int) def findGold(mNow, i0, j0, goldNow): subAns = goldNow for k in range(4): i1 = i0 + di[k] j1 = j0 + dj[k] if ( i1 < 0 or j1 < 0 or i1 == iMax or j1 == jMax or grid[i1][j1] == 0 or mNow & 1 << goldNumDict[i1, j1] ): continue subAns = max( subAns, findGold( mNow | 1 << goldNumDict[i1, j1], i1, j1, goldNow + grid[i1][j1] ), ) dp[mNow] = max(subAns, dp[mNow]) return subAns ans = 0 for i in range(iMax): for j in range(jMax): if grid[i][j] != 0: ans = max(ans, findGold(1 << goldNumDict[i, j], i, j, grid[i][j])) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: gold = 0 def dfs(self, grid, i, j, current): if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] == 0: return current += grid[i][j] temp = grid[i][j] grid[i][j] = 0 self.dfs(grid, i, j - 1, current) self.dfs(grid, i, j + 1, current) self.dfs(grid, i + 1, j, current) self.dfs(grid, i - 1, j, current) grid[i][j] = temp self.gold = max(self.gold, current) def getMaximumGold(self, grid: List[List[int]]) -> int: for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: current = 0 self.dfs(grid, i, j, current) return self.gold
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) visited = [([False] * n) for _ in range(m)] self.ans = 0 def dfs(i, j): if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] or grid[i][j] == 0: return 0 visited[i][j] = True res = grid[i][j] + max( dfs(i - 1, j), dfs(i + 1, j), dfs(i, j - 1), dfs(i, j + 1) ) visited[i][j] = False self.ans = max(self.ans, res) return res for i in range(m): for j in range(n): if grid[i][j]: dfs(i, j) return self.ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: def sorround(curr): posts = [[1, 0], [0, 1], [-1, 0], [0, -1]] for i in posts: ii = i[0] + curr[0] jj = i[1] + curr[1] if ( ii < len(grid) and ii >= 0 and jj >= 0 and jj < len(grid[0]) and grid[ii][jj] != 0 ): yield ii, jj def depth_first_search(cur): cur_sum = grid[cur[0]][cur[1]] ans = 0 for i in sorround(cur): if i not in visited or visited[i] == False: visited[i] = True mx = depth_first_search(i) ans = max(ans, mx) visited[i] = False return cur_sum + ans cans = 0 visited = {} for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: visited[i, j] = True cs = depth_first_search((i, j)) cans = max(cs, cans) visited[i, j] = False return cans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: if not grid or not grid[0]: return 0 self.max_gold = 0 rows = len(grid) cols = len(grid[0]) for row in range(rows): for col in range(cols): self.dfs(grid, row, col, 0) return self.max_gold def dfs(self, grid, row, col, gold): rows = len(grid) cols = len(grid[0]) if row < 0 or row >= rows or col < 0 or col >= cols or grid[row][col] == 0: return prev_gold = grid[row][col] grid[row][col] = 0 gold += prev_gold self.max_gold = max(self.max_gold, gold) for drow, dcol in [ (row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1), ]: self.dfs(grid, drow, dcol, gold) grid[row][col] = prev_gold
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: allGod = -1 def getMaximumGold(self, grid: List[List[int]]) -> int: def helper(maxgold, i, j): if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] > 0: temp = grid[i][j] self.allGod = max(self.allGod, maxgold + temp) grid[i][j] = 0 helper(maxgold + temp, i - 1, j) helper(maxgold + temp, i + 1, j) helper(maxgold + temp, i, j - 1) helper(maxgold + temp, i, j + 1) grid[i][j] = temp for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] > 0: helper(0, i, j) return self.allGod
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def __init__(self): self.moves = [(-1, 0), (1, 0), (0, -1), (0, 1)] def dfs(self, r, c): if r < 0 or r == self.m or c < 0 or c == self.n or self.grid[r][c] == 0: return 0 original = self.grid[r][c] self.grid[r][c] = 0 maxGold = max(self.dfs(r + x, c + y) for x, y in self.moves) self.grid[r][c] = original return maxGold + original def getMaximumGold(self, grid: List[List[int]]) -> int: self.m = len(grid) self.n = len(grid[0]) self.grid = grid return max(self.dfs(i, j) for i in range(self.m) for j in range(self.n))
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: M, N = len(grid), len(grid[0]) self.res = 0 def dfs(grid, i, j, cur): nbr = [(i, j + 1), (i, j - 1), (i - 1, j), (i + 1, j)] temp = grid[i][j] self.res = max(self.res, cur + temp) for r, c in nbr: if 0 <= r < M and 0 <= c < N and grid[r][c]: grid[i][j] = 0 dfs(grid, r, c, cur + temp) grid[i][j] = temp for i in range(M): for j in range(N): if grid[i][j]: dfs(grid, i, j, 0) print(("res = ", self.res)) return self.res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) grid_visited = [[(0) for i in range(n)] for i in range(m)] max_gold = 0 for i in range(m): for j in range(n): if grid[i][j] != 0: temp = self.start_mine(grid, grid_visited, i, j) max_gold = max(max_gold, temp) return max_gold def start_mine(self, grid, grid_visited, i, j): total = grid[i][j] max_choice = 0 grid_visited[i][j] = 1 if i - 1 >= 0 and grid[i - 1][j] != 0 and grid_visited[i - 1][j] == 0: temp = self.start_mine(grid, grid_visited, i - 1, j) max_choice = max(temp, max_choice) if i + 1 < len(grid) and grid[i + 1][j] != 0 and grid_visited[i + 1][j] == 0: temp = self.start_mine(grid, grid_visited, i + 1, j) max_choice = max(temp, max_choice) if j - 1 >= 0 and grid[i][j - 1] != 0 and grid_visited[i][j - 1] == 0: temp = self.start_mine(grid, grid_visited, i, j - 1) max_choice = max(temp, max_choice) if j + 1 < len(grid[0]) and grid[i][j + 1] != 0 and grid_visited[i][j + 1] == 0: temp = self.start_mine(grid, grid_visited, i, j + 1) max_choice = max(temp, max_choice) grid_visited[i][j] = 0 return total + max_choice
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: ans = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: ans = max(ans, self.findGold(grid, i, j)) return ans def findGold(self, grid, row, col): if ( not (0 <= row < len(grid) and 0 <= col < len(grid[0])) or grid[row][col] == 0 ): return 0 ans = 0 temp = grid[row][col] grid[row][col] = 0 for i, j in [(1, 0), (-1, 0), (0, 1), (0, -1)]: ans = max(ans, self.findGold(grid, row + i, col + j)) grid[row][col] = temp return ans + temp
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: nrow, ncol = len(grid), len(grid[0]) max_len = 0 def dfs(trajectory, r, c): nonlocal max_len count_neighbor = 0 for dr, dc in [[0, 1], [0, -1], [1, 0], [-1, 0]]: nr, nc = r + dr, c + dc if ( 0 <= nr < nrow and 0 <= nc < ncol and grid[nr][nc] != 0 and (nr, nc) not in trajectory ): count_neighbor += 1 trajectory.add((nr, nc)) dfs(trajectory, nr, nc) trajectory.remove((nr, nc)) if count_neighbor == 0: max_len = max(max_len, sum(grid[i][j] for i, j in trajectory)) max_len = 0 for r in range(nrow): for c in range(ncol): if grid[r][c] != 0: dfs(set([(r, c)]), r, c) return max_len
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: self.ans = 0 def dfs(grid, i, j, res): res = res + grid[i][j] tmp = grid[i][j] grid[i][j] = 0 for r, c in ((i + 1, j), (i, j + 1), (i - 1, j), (i, j - 1)): if 0 <= r < len(grid) and 0 <= c < len(grid[0]): if grid[r][c]: dfs(grid, r, c, res) grid[i][j] = tmp self.ans = max(self.ans, res) for i in range(0, len(grid)): for j in range(0, len(grid[0])): if grid[i][j]: dfs(grid, i, j, 0) return self.ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: R, C = len(grid), len(grid[0]) O = [] def check(r, c, Z): if -1 < r < R and -1 < c < C and (r, c) not in Z: return True return False def bt(r, c, res, Z): if grid[r][c] == 0: O.append(res) return else: a = grid[r][c] if check(r + 1, c, Z + [(r, c)]) == True: bt(r + 1, c, res + a, Z + [(r, c)]) if check(r - 1, c, Z + [(r, c)]) == True: bt(r - 1, c, res + a, Z + [(r, c)]) if check(r, c + 1, Z + [(r, c)]) == True: bt(r, c + 1, res + a, Z + [(r, c)]) if check(r, c - 1, Z + [(r, c)]) == True: bt(r, c - 1, res + a, Z + [(r, c)]) return for i in range(R): for j in range(C): if grid[i][j] != 0: bt(i, j, 0, []) return max(O)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR LIST VAR VAR RETURN FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER LIST RETURN FUNC_CALL VAR VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) ones = set([]) start = [] answer = 0 for i in range(n): for j in range(m): if grid[i][j] != 0: ones.add((i, j)) start.append([(i, j), grid[i][j], set([(i, j)])]) answer = max(answer, grid[i][j]) nearby = {} for a, b in ones: nearby[a, b] = [] for c, d in [(a + 1, b), (a - 1, b), (a, b - 1), (a, b + 1)]: if (c, d) in ones: if a == c and abs(b - d) == 1 or abs(a - c) == 1 and b == d: nearby[a, b].append((c, d)) while True: next = [] for a, b, c in start: for a2 in nearby[a]: if a2 not in c: b2 = b + grid[a2[0]][a2[1]] c2 = c.union(set([a2])) answer = max(answer, b2) next.append([a2, b2, c2]) start = next if len(start) == 0: break return answer
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR ASSIGN VAR VAR VAR LIST FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid): path = [] total = 0 self.rLen = len(grid) self.cLen = len(grid[0]) for i in range(self.rLen): for j in range(self.cLen): if grid[i][j] != 0: retTotal, retPath = self.dfs(grid, i, j) if retTotal > total: total = retTotal path = retPath return total def dfs(self, grid, i, j): tmp = grid[i][j] grid[i][j] = 0 total = 0 path = [] for newI, newJ in [[i + 1, j], [i - 1, j], [i, j + 1], [i, j - 1]]: if ( 0 <= newI < self.rLen and 0 <= newJ < self.cLen and grid[newI][newJ] != 0 ): retTotal, retPath = self.dfs(grid, newI, newJ) if retTotal > total: total = retTotal path = retPath grid[i][j] = tmp return total + tmp, path + [[i, j]]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR LIST LIST BIN_OP VAR NUMBER VAR LIST BIN_OP VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR BIN_OP VAR LIST LIST VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m = len(grid) - 1 n = len(grid[0]) - 1 def dfs(i, j, visited): val = 0 visited[i][j] = True for co in ((-1, 0), (1, 0), (0, -1), (0, 1)): x = i + co[0] y = j + co[1] if ( 0 <= x <= m and 0 <= y <= n and grid[x][y] != 0 and visited[x][y] == False ): val = max(val, dfs(x, y, visited)) visited[i][j] = False return val + grid[i][j] vis = [([False] * (n + 1)) for _ in range(m + 1)] res = 0 for i in range(m + 1): for j in range(n + 1): if grid[i][j] != 0: res = max(res, dfs(i, j, vis)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: nR = len(grid) nC = len(grid[0]) vis = [[(0) for i in range(nC)] for j in range(nR)] steps = [[0, -1], [-1, 0], [1, 0], [0, 1]] _max = 0 for r in range(nR): for c in range(nC): cell = grid[r][c] if cell == 0: vis[r][c] = -1 for r in range(nR): for c in range(nC): if vis[r][c] == -1: continue _max = max(_max, self.dfs(grid, vis, r, c, nR, nC, steps)) return _max def dfs(self, grid, vis, r, c, nR, nC, steps): vis[r][c] = -1 cell = grid[r][c] _max = cell for step in steps: _r = r + step[0] _c = c + step[1] if 0 <= _r < nR and 0 <= _c < nC and grid[_r][_c] != 0 and vis[_r][_c] == 0: _max = max(_max, cell + self.dfs(grid, vis, _r, _c, nR, nC, steps)) vis[r][c] = 0 return _max
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
DIR = (-1, 0), (1, 0), (0, -1), (0, 1) class Solution: def getMaximumGold(self, grid): res = 0 m, n = len(grid), len(grid[0]) for x in range(m): for y in range(n): if grid[x][y] > 0: res = max(res, self.dfs(grid, x, y, {(x, y)})) return res def dfs(self, grid, x, y, visited): m, n = len(grid), len(grid[0]) res = grid[x][y] for dx, dy in DIR: nx, ny = x + dx, y + dy if ( 0 <= nx <= m - 1 and 0 <= ny <= n - 1 and grid[nx][ny] > 0 and (nx, ny) not in visited ): visited.add((nx, ny)) res = max(res, self.dfs(grid, nx, ny, visited) + grid[x][y]) visited.remove((nx, ny)) return res
ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: visited = set() m, n = len(grid), len(grid[0]) def isValid(x, y): return 0 <= x < m and 0 <= y < n dirs = [(-1, 0), (1, 0), (0, 1), (0, -1)] def dfs(x, y): if grid[x][y] == 0: return 0 visited.add((x, y)) ans = 0 for dx, dy in dirs: if isValid(x + dx, y + dy) and (x + dx, y + dy) not in visited: ans = max(ans, dfs(x + dx, y + dy)) visited.remove((x, y)) return ans + grid[x][y] ans = 0 for i in range(m): for j in range(n): ans = max(ans, dfs(i, j)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) l = [(1, 0), (-1, 0), (0, 1), (0, -1)] vis = set() res = [0] z = -float("inf") def fn(a, b): z = -float("inf") for c, d in l: x, y = a + c, b + d if x < 0 or x >= m or y < 0 or y >= n: continue if (x, y) not in vis and grid[x][y] > 0: vis.add((x, y)) res[0] += grid[x][y] z = max(z, fn(x, y)) res[0] -= grid[x][y] vis.remove((x, y)) if z == -float("inf"): return res[0] return z for x in range(m): for y in range(n): if grid[x][y] > 0: vis.add((x, y)) res[0] += grid[x][y] z = max(z, fn(x, y)) res[0] -= grid[x][y] vis.remove((x, y)) if z == -float("inf"): return 0 return z
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: def dfs(i, j, cur): nonlocal mx cur += grid[i][j] mx = max(mx, cur) path.add((i, j)) for I, J in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]: if ( 0 <= I < len(grid) and 0 <= J < len(grid[0]) and grid[I][J] != 0 and (I, J) not in path ): dfs(I, J, cur) path.remove((i, j)) if not grid: return 0 mx = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: path = set() dfs(i, j, 0) return mx
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: max_gold = 0 rows = len(grid) cols = len(grid[0]) for row in range(rows): for col in range(cols): if grid[row][col] > 0: max_gold = max(max_gold, self.get_gold(grid, row, col)) return max_gold def get_gold(self, grid, row, col): if ( row < 0 or row >= len(grid) or col < 0 or col >= len(grid[row]) or grid[row][col] == 0 ): return 0 ori = grid[row][col] gold = 0 grid[row][col] = 0 gold = max(gold, self.get_gold(grid, row + 1, col)) gold = max(gold, self.get_gold(grid, row - 1, col)) gold = max(gold, self.get_gold(grid, row, col + 1)) gold = max(gold, self.get_gold(grid, row, col - 1)) grid[row][col] = ori return gold + ori
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: maxy = [0] print(grid) def gold(grid, summ, i, j): if ( i <= len(grid) - 1 and i > -1 and j <= len(grid[0]) - 1 and j > -1 and grid[i][j] != 0 ): temp = grid[i][j] grid[i][j] = 0 gold(grid, summ + temp, i + 1, j) gold(grid, summ + temp, i, j + 1) gold(grid, summ + temp, i - 1, j) gold(grid, summ + temp, i, j - 1) grid[i][j] = temp else: maxy[0] = max(maxy[0], summ) for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: gold(grid, 0, i, j) return maxy[0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR NUMBER VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, g: List[List[int]]) -> int: def dfs(i, j, visited): if not ( 0 <= i < len(g) and 0 <= j < len(g[0]) and g[i][j] != 0 and (i, j) not in visited ): return 0 visited.add((i, j)) res = g[i][j] + max( 0, max( dfs(i + x, j + y, visited) for x, y in [[-1, 0], [1, 0], [0, 1], [0, -1]] ), ) visited.remove((i, j)) return res return max(dfs(i, j, set()) for i in range(len(g)) for j in range(len(g[0])))
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: nrow, ncol = len(grid), len(grid[0]) def getMaximumGold_helper(grid, i, j): if i >= nrow or i < 0 or j >= ncol or j < 0: return 0 if grid[i][j] == 0: return 0 cur = grid[i][j] grid[i][j] = 0 res = cur + max( getMaximumGold_helper(grid, i, j + 1), getMaximumGold_helper(grid, i, j - 1), getMaximumGold_helper(grid, i + 1, j), getMaximumGold_helper(grid, i - 1, j), ) grid[i][j] = cur return res max_gold = 0 for i in range(nrow): for j in range(ncol): cur_gold = getMaximumGold_helper(grid, i, j) max_gold = max(max_gold, cur_gold) return max_gold
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: if not grid: return 0 def dfs(grid, i, j): if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0: return 0 res = grid[i][j] grid[i][j] = 0 cost = res + max( dfs(grid, i + 1, j), dfs(grid, i - 1, j), dfs(grid, i, j + 1), dfs(grid, i, j - 1), ) grid[i][j] = res return cost max_gold = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: max_gold = max(max_gold, dfs(grid, i, j)) return max_gold
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def dfs(self, pos, sum): if ( pos in self.visit or pos[0] < 0 or pos[0] >= self.M or pos[1] < 0 or pos[1] >= self.N or not self.grid[pos[0]][pos[1]] ): return self.visit.add(pos) sum += self.grid[pos[0]][pos[1]] x, y = pos self.maxGold = max(self.maxGold, sum) [ self.dfs(node, sum) for node in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)] ] self.visit.remove(pos) def getMaximumGold(self, grid: List[List[int]]): self.maxGold = 0 self.M = len(grid) self.N = len(grid[0]) self.grid = grid for beginX in range(self.M): for beginY in range(self.N): if grid[beginX][beginY] > 0: self.visit = set() self.dfs((beginX, beginY), 0) return self.maxGold
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) def collect(i, j): if not (0 <= i < m and 0 <= j < n and grid[i][j]): return 0 gold, grid[i][j] = grid[i][j], 0 choices = [ collect(i + di, j + dj) for di, dj in [(1, 0), (-1, 0), (0, 1), (0, -1)] ] grid[i][j] = gold return max(choices) + gold return max(collect(i, j) for i in range(m) for j in range(n))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def __init__(self): self.max_gold = float("-Inf") def getMaximumGold(self, grid: List[List[int]]) -> int: if not grid: return 0 def dfs(current, i, j): if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] == 0: return current += grid[i][j] temp = grid[i][j] grid[i][j] = 0 self.max_gold = max(current, self.max_gold) dfs(current, i - 1, j) dfs(current, i + 1, j) dfs(current, i, j - 1) dfs(current, i, j + 1) grid[i][j] = temp for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: dfs(0, i, j) return self.max_gold if self.max_gold != float("-Inf") else 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: def goDfsGo(row, col): ret_max = 0 temp_val = grid[row][col] grid[row][col] = False if ( row + 1 < len(grid) and grid[row + 1][col] != False and grid[row + 1][col] != 0 ): ret_max = max(ret_max, temp_val + goDfsGo(row + 1, col)) if row - 1 >= 0 and grid[row - 1][col] != False and grid[row - 1][col] != 0: ret_max = max(ret_max, temp_val + goDfsGo(row - 1, col)) if ( col + 1 < len(grid[0]) and grid[row][col + 1] != False and grid[row][col + 1] != 0 ): ret_max = max(ret_max, temp_val + goDfsGo(row, col + 1)) if col - 1 >= 0 and grid[row][col - 1] != False and grid[row][col - 1] != 0: ret_max = max(ret_max, temp_val + goDfsGo(row, col - 1)) grid[row][col] = temp_val return max(ret_max, grid[row][col]) mah_max = 0 for row in range(len(grid)): for col in range(len(grid[0])): if grid[row][col] != 0: mah_max = max(mah_max, goDfsGo(row, col)) return mah_max
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: self.dirs = (1, 0), (0, 1), (-1, 0), (0, -1) self.nrow = len(grid) self.ncol = len(grid[0]) max_gold = 0 for i in range(self.nrow): for j in range(self.ncol): if grid[i][j] != 0: max_seen = self._dfs(grid, i, j, set()) if max_seen > max_gold: max_gold = max_seen return max_gold def _dfs(self, grid, i, j, visited): child_max = [] visited.add((i, j)) for dx, dy in self.dirs: if 0 <= i + dx < self.nrow and 0 <= j + dy < self.ncol: if grid[i + dx][j + dy] != 0 and (i + dx, j + dy) not in visited: dir_sum = self._dfs(grid, i + dx, j + dy, visited) child_max.append(dir_sum) visited.remove((i, j)) max_child = max(child_max) if child_max else 0 return grid[i][j] + max_child
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def dfs(self, grid, i, j, m, n): tmp = 0 if grid[i][j] == 0 or self.visited[i][j] == 1: return 0 self.visited[i][j] = 1 for x, y in self.direction: x = i + x y = j + y if ( x >= 0 and y >= 0 and x < m and y < n and grid[x][y] != 0 and self.visited[x][y] != 1 ): tmp = max(tmp, self.dfs(grid, x, y, m, n)) self.visited[i][j] = 0 return tmp + grid[i][j] def getMaximumGold(self, grid: List[List[int]]) -> int: self.direction = [[0, 1], [0, -1], [1, 0], [-1, 0]] m = len(grid) n = len(grid[0]) self.visited = [[(0) for _ in range(n)] for _ in range(m)] res = 0 for i in range(m): for j in range(n): res = max(res, self.dfs(grid, i, j, m, n)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: di = [(-1, 0), (0, 1), (1, 0), (0, -1)] ans = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] != 0: v = self.bfs(grid, i, j, di) ans = max(ans, v) return ans def bfs(self, A, r, c, di): q = collections.deque([[r, c, A[r][c], {(r, c)}]]) mx = 0 while q: x, y, n, seen = q.popleft() seen.add((x, y)) mx = max(mx, n) for i, j in di: nx, ny = x + i, y + j if ( (nx, ny) not in seen and 0 <= nx < len(A) and 0 <= ny < len(A[0]) and A[nx][ny] != 0 ): q.append([nx, ny, n + A[nx][ny], seen.copy()]) return mx
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST LIST VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR RETURN VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: def q(g, i, j, t, s, vis): if ( i >= len(g) or j >= len(g[0]) or i < 0 or j < 0 or g[i][j] == 0 or vis[i][j] == 1 ): s[0] = max(s[0], t[0]) return temp = g[i][j] g[i][j] = 0 t[0] += temp q(g, i, j + 1, t, s, vis) q(g, i + 1, j, t, s, vis) q(g, i - 1, j, t, s, vis) q(g, i, j - 1, t, s, vis) t[0] -= temp g[i][j] = temp s = [0] t = [0] vis = [[(0) for i in range(len(grid[0]))] for j in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): t = [0] q(grid, i, j, t, s, vis) return s[0]
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: def neighbor(r, c): for dr, dc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)): if 0 <= dr < nrow and 0 <= dc < ncol: yield dr, dc self.cur_gold = 0 self.max_gold = 0 def backtrack(r, c): if grid[r][c] == 0: return gold_cnt = grid[r][c] self.cur_gold += gold_cnt self.max_gold = max(self.max_gold, self.cur_gold) grid[r][c] = 0 for dr, dc in neighbor(r, c): backtrack(dr, dc) grid[r][c] = gold_cnt self.cur_gold -= gold_cnt nrow, ncol = len(grid), len(grid[0]) for r in range(nrow): for c in range(ncol): backtrack(r, c) return self.max_gold
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR EXPR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: ROWS = len(grid) COLS = len(grid[0]) self.mx = 0 seen = set() def dfs(r, c): if ( not 0 <= r < ROWS or not 0 <= c < COLS or (r, c) in seen or grid[r][c] == 0 ): return 0 val = grid[r][c] seen.add((r, c)) mx = max(dfs(r + 1, c), dfs(r - 1, c), dfs(r, c + 1), dfs(r, c - 1)) seen.remove((r, c)) return val + mx for r in range(ROWS): for c in range(COLS): if grid[r][c] != 0: self.mx = max(self.mx, dfs(r, c)) return self.mx
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) def dfs(x, y, visited, res): maxi = res[:] for i, j in [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]: if ( i >= 0 and i < m and j >= 0 and j < n and grid[i][j] > 0 and (i, j) not in visited ): visited.add((i, j)) res.append(grid[i][j]) result = dfs(i, j, visited, res) maxi = maxi if sum(maxi) > sum(result) else result res.pop() visited.remove((i, j)) return maxi maxi = 0 for i in range(m): for j in range(n): if grid[i][j] > 0: visited = set() res = [] res.append(grid[i][j]) visited.add((i, j)) result = dfs(i, j, visited, res) maxi = max(maxi, sum(result)) return maxi
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR LIST BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: if not grid: return 0 res = 0 M = len(grid) N = len(grid[0]) cords = [[0, 1], [0, -1], [-1, 0], [1, 0]] def recur(x, y): if x < 0 or x >= M or y < 0 or y >= N or grid[x][y] == 0: return 0 temp = grid[x][y] grid[x][y] = 0 total = 0 for dx, dy in cords: dx += x dy += y total = max(total, recur(dx, dy)) grid[x][y] = temp return total + temp for i in range(M): for j in range(N): if grid[i][j] != 0: res = max(recur(i, j), res) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: nrow, ncol = len(grid), len(grid[0]) possible_starts = [ (i, j) for i in range(nrow) for j in range(ncol) if grid[i][j] != 0 ] max_gold = 0 for start in possible_starts: path_stack = [start] cell_to_other_paths = collections.defaultdict(list) x, y = start cumulated_gold = grid[x][y] while path_stack: found_next = False if x - 1 >= 0 and grid[x - 1][y] != 0 and (x - 1, y) not in path_stack: found_next = True tmp_x, tmp_y = x - 1, y if ( x + 1 < nrow and grid[x + 1][y] != 0 and (x + 1, y) not in path_stack ): if found_next: cell_to_other_paths[x, y].append((x + 1, y)) else: found_next = True tmp_x, tmp_y = x + 1, y if y - 1 >= 0 and grid[x][y - 1] != 0 and (x, y - 1) not in path_stack: if found_next: cell_to_other_paths[x, y].append((x, y - 1)) else: found_next = True tmp_x, tmp_y = x, y - 1 if ( y + 1 < ncol and grid[x][y + 1] != 0 and (x, y + 1) not in path_stack ): if found_next: cell_to_other_paths[x, y].append((x, y + 1)) else: found_next = True tmp_x, tmp_y = x, y + 1 if found_next: path_stack.append((tmp_x, tmp_y)) cumulated_gold += grid[tmp_x][tmp_y] x, y = tmp_x, tmp_y else: max_gold = max(max_gold, cumulated_gold) while ( path_stack and len(cell_to_other_paths.get(path_stack[-1], [])) == 0 ): pop_x, pop_y = path_stack.pop(-1) cumulated_gold -= grid[pop_x][pop_y] if not path_stack: break x, y = cell_to_other_paths[path_stack[-1]].pop(0) path_stack.append((x, y)) cumulated_gold += grid[x][y] return max_gold
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
In a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty. Return the maximum amount of gold you can collect under the conditions: Every time you are located in a cell you will collect all the gold in that cell. From your position you can walk one step to the left, right, up or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.   Example 1: Input: grid = [[0,6,0],[5,8,7],[0,9,0]] Output: 24 Explanation: [[0,6,0], [5,8,7], [0,9,0]] Path to get the maximum gold, 9 -> 8 -> 7. Example 2: Input: grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]] Output: 28 Explanation: [[1,0,7], [2,0,6], [3,4,5], [0,3,0], [9,0,20]] Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.   Constraints: 1 <= grid.length, grid[i].length <= 15 0 <= grid[i][j] <= 100 There are at most 25 cells containing gold.
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: if not grid: return 0 self.answer = 0 def dfs(i, j): if i < 0 or i >= len(grid): return 0 if j < 0 or j >= len(grid[0]): return 0 if grid[i][j] == 0: return 0 temp = grid[i][j] grid[i][j] = 0 storage = temp + max( dfs(i + 1, j), dfs(i - 1, j), dfs(i, j - 1), dfs(i, j + 1) ) self.answer = max(self.answer, storage) grid[i][j] = temp return storage for i in range(len(grid)): for j in range(len(grid[0])): dfs(i, j) return self.answer
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR