description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, 0, 0] for num in nums: for x in dp[:]: dp[(x + num) % 3] = max(dp[(x + num) % 3], x + num) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: nums = sorted(nums) l0 = [i for i in nums if i % 3 == 0] l1 = [i for i in nums if i % 3 == 1] l2 = [i for i in nums if i % 3 == 2] print((l0, l1, l2)) total_sum = sum(nums) res = total_sum if total_sum % 3 != 0: if total_sum % 3 == 1: res1 = sum(l0) + sum(l1[1:]) + sum(l2) res2 = sum(l0) + sum(l1) + sum(l2[2:]) elif total_sum % 3 == 2: res1 = sum(l0) + sum(l1[2:]) + sum(l2) res2 = sum(l0) + sum(l1) + sum(l2[1:]) if res1 % 3 == 0 and res2 % 3 == 0: res = max(res1, res2) elif res1 % 3 == 0: res = res1 else: res = res2 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, -1, -1] for v in nums: new_dp = list(dp) if dp[v % 3] < 0: new_dp[v % 3] = v for i in range(0, 3): new_i = (i + v) % 3 if dp[i] >= 0: new_dp[new_i] = max(dp[new_i], dp[i] + v) dp = new_dp return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [([0] * 3) for _ in range(len(nums) + 1)] for i, n in enumerate(nums): r = n % 3 for j in range(3): if dp[i][j] > 0: dp[i + 1][(j + r) % 3] = max(dp[i][j] + n, dp[i][(j + r) % 3]) else: dp[i + 1][(j + r) % 3] = dp[i][(j + r) % 3] dp[i + 1][r] = max(n, dp[i + 1][r]) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) zero, one, two = [0] * (n + 1), [0] * (n + 1), [0] * (n + 1) one[0] = two[0] = float("-inf") for i in range(1, n + 1): if nums[i - 1] % 3 == 0: zero[i] = zero[i - 1] + nums[i - 1] one[i] = one[i - 1] + nums[i - 1] if one[i - 1] > 0 else 0 two[i] = two[i - 1] + nums[i - 1] if two[i - 1] > 0 else 0 elif nums[i - 1] % 3 == 1: zero[i] = max(two[i - 1] + nums[i - 1], zero[i - 1]) two[i] = max(two[i - 1], one[i - 1] + nums[i - 1]) one[i] = max(one[i - 1], zero[i - 1] + nums[i - 1]) else: zero[i] = max(one[i - 1] + nums[i - 1], zero[i - 1]) two[i] = max(zero[i - 1] + nums[i - 1], two[i - 1]) one[i] = max(two[i - 1] + nums[i - 1], one[i - 1]) return zero[-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: r0, r1, r2 = 0, 0, 0 for val in nums: r = val % 3 if r == 0: new_r0 = r0 + val new_r1 = r1 + val if r1 else 0 new_r2 = r2 + val if r2 else 0 elif r == 1: new_r0 = max(r2 + val, r0) if r2 else r0 new_r1 = max(r0 + val, r1) new_r2 = max(r1 + val, r2) if r1 else r2 elif r == 2: new_r0 = max(r1 + val, r0) if r1 else r0 new_r1 = max(r2 + val, r1) if r2 else r1 new_r2 = max(r0 + val, r2) r0, r1, r2 = new_r0, new_r1, new_r2 return r0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [[0, -1, -1] for _ in range(len(nums) + 1)] for i in range(1, len(nums) + 1): r = nums[i - 1] % 3 for s in range(3): if dp[i - 1][s] >= 0: dp[i][(r + s) % 3] = max( dp[i - 1][(r + s) % 3], dp[i - 1][s] + nums[i - 1] ) dp[i][s] = max(dp[i - 1][s], dp[i][s]) return dp[len(nums)][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: f = [([0] * 3) for i in range(len(nums))] f[0][nums[0] % 3] = nums[0] for i in range(1, len(nums)): for j in range(3): include = f[i - 1][(j - nums[i]) % 3] + nums[i] if include % 3 == j: f[i][j] = max(f[i - 1][j], include) else: f[i][j] = f[i - 1][j] return f[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: zero = one = two = 0 for n in nums: nz, no, nt = zero + n, one + n, two + n zero = max( zero, nz if nz % 3 == 0 else 0, no if no % 3 == 0 else 0, nt if nt % 3 == 0 else 0, ) one = max( one, nz if nz % 3 == 1 else 0, no if no % 3 == 1 else 0, nt if nt % 3 == 1 else 0, ) two = max( two, nz if nz % 3 == 2 else 0, no if no % 3 == 2 else 0, nt if nt % 3 == 2 else 0, ) return zero
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: cur = [0] + [-1] * 2 tmp = cur[:] for x in nums: for i in range(3): idx = (i + x) % 3 if cur[i] >= 0: tmp[idx] = max(cur[i] + x, cur[idx]) cur = tmp[:] return cur[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, 0, 0] for n in nums: if n % 3 == 0: dp0 = dp[0] + n if dp[1] > 0: dp1 = dp[1] + n else: dp1 = dp[1] if dp[2] > 0: dp2 = dp[2] + n else: dp2 = dp[2] elif n % 3 == 1: if dp[2] > 0: dp0 = max(dp[0], dp[2] + n) else: dp0 = dp[0] if dp[0] > 0: dp1 = max(dp[1], dp[0] + n) else: dp1 = max(dp[1], n) if dp[1] > 0: dp2 = max(dp[2], dp[1] + n) else: dp2 = dp[2] elif n % 3 == 2: if dp[1] > 0: dp0 = max(dp[0], dp[1] + n) else: dp0 = dp[0] if dp[2] > 0: dp1 = max(dp[1], dp[2] + n) else: dp1 = dp[1] if dp[0] > 0: dp2 = max(dp[2], dp[0] + n) else: dp2 = max(dp[2], n) dp[0] = dp0 dp[1] = dp1 dp[2] = dp2 return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: total = sum(nums) mod = total % 3 if mod == 0: return total min_2 = defaultdict(list) for num in nums: if num % 3 == 1: min_2[1].append(num) min_2[1].sort() if len(min_2[1]) > 2: min_2[1].pop() elif num % 3 == 2: min_2[2].append(num) min_2[2].sort() if len(min_2[2]) > 2: min_2[2].pop() res = 0 if mod == 1: if len(min_2[1]) >= 1: res = total - min_2[1][0] if len(min_2[2]) >= 2: res = max(res, total - min_2[2][0] - min_2[2][1]) return res if len(min_2[2]) >= 1: res = total - min_2[2][0] if len(min_2[1]) >= 2: res = max(res, total - min_2[1][0] - min_2[1][1]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: if not nums: return 0 dp = [[(0) for _ in range(3)] for _ in range(len(nums) + 1)] dp[0][1] = float("-inf") dp[0][2] = float("-inf") for i in range(1, len(nums) + 1): num = nums[i - 1] rem = num % 3 if rem == 0: dp[i][0] = max(dp[i - 1][0], dp[i - 1][0] + num) dp[i][1] = max(dp[i - 1][1], dp[i - 1][1] + num) dp[i][2] = max(dp[i - 1][2], dp[i - 1][2] + num) elif rem == 1: dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] + num) dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + num) dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + num) else: dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + num) dp[i][1] = max(dp[i - 1][1], dp[i - 1][2] + num) dp[i][2] = max(dp[i - 1][2], dp[i - 1][0] + num) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) if n == 0: return 0 nums.sort() m3 = [] m2 = [] m1 = [] for i in nums: if i % 3 == 0: m3.append(i) elif i % 3 == 2: m2.append(i) elif i % 3 == 1: m1.append(i) total = sum(nums) if total % 3 == 0: return total elif total % 3 == 2: comp1 = 0 comp2 = 0 if len(m2) > 0: comp1 = total - m2[0] if len(m1) > 1: comp2 = total - m1[0] - m1[1] return max(comp1, comp2) elif total % 3 == 1: comp1 = 0 comp2 = 0 if len(m1) > 0: comp1 = total - m1[0] if len(m2) > 1: comp2 = total - m2[0] - m2[1] return max(comp1, comp2)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: res = 0 total = sum(nums) remove = tuple(sorted(n for n in nums if n % 3)) q = collections.deque([(total, 0)]) visited = set([0]) while q: t, r = q.popleft() if not t % 3: res = max(res, t) continue if t <= res: continue for i in range(len(remove)): if not 1 << i & r: b = 1 << i | r if b not in visited: visited.add(b) q.append((t - remove[i], b)) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution1: def maxSumDivThree(self, nums: List[int]) -> int: seen = [0, 0, 0] for a in nums: for i in seen[:]: seen[(i + a) % 3] = max(seen[(i + a) % 3], i + a) return seen[0] class Solution: def maxSumDivThree(self, nums: List[int]) -> int: mod_1, mod_2, res, remove = [], [], 0, float("inf") for i in nums: if i % 3 == 0: res += i if i % 3 == 1: mod_1 += [i] if i % 3 == 2: mod_2 += [i] mod_1.sort(reverse=True) mod_2.sort(reverse=True) tmp = sum(mod_1) + sum(mod_2) if tmp % 3 == 0: return res + tmp elif tmp % 3 == 1: if len(mod_1): remove = min(remove, mod_1[-1]) if len(mod_2) > 1: remove = min(mod_2[-1] + mod_2[-2], remove) elif tmp % 3 == 2: if len(mod_2): remove = min(remove, mod_2[-1]) if len(mod_1) > 1: remove = min(mod_1[-1] + mod_1[-2], remove) return res + tmp - remove
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR NUMBER VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR LIST LIST NUMBER FUNC_CALL VAR STRING FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: m = sum(nums) N = len(nums) @lru_cache(None) def dp(i, p): if i == N: return 0 if p == 0 else -float("inf") a = dp(i + 1, p) b = dp(i + 1, (p + nums[i]) % 3) + nums[i] return max(a, b) return dp(0, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: nums.sort() quotient = [] remainder = [] ret = 0 for num in nums: quotient.append(num // 3 * 3) remainder.append(num % 3) ret += num remainder_sum = ret % 3 if remainder_sum == 0: return ret elif remainder_sum == 1: remainder1 = 0 remainder1_count = 0 remainder2 = 0 remainder2_count = 0 for i in range(len(nums)): if remainder[i] == 1: if remainder1_count >= 1: continue remainder1_count += 1 remainder1 += nums[i] if remainder[i] == 2: if remainder2_count >= 2: continue remainder2_count += 1 remainder2 += nums[i] if remainder2_count >= 1 and remainder1_count >= 2: break if remainder2_count == 2 and remainder1_count == 1: return ret - min(remainder1, remainder2) elif remainder2_count == 2: return ret - remainder2 elif remainder1_count == 1: return ret - remainder1 else: return 0 else: remainder1 = 0 remainder1_count = 0 remainder2 = 0 remainder2_count = 0 for i in range(len(nums)): if remainder[i] == 1: if remainder1_count >= 2: continue remainder1_count += 1 remainder1 += nums[i] if remainder[i] == 2: if remainder2_count >= 1: continue remainder2_count += 1 remainder2 += nums[i] if remainder2_count >= 1 and remainder1_count >= 2: break if remainder2_count == 1 and remainder1_count == 2: return ret - min(remainder1, remainder2) elif remainder2_count == 1: return ret - remainder2 elif remainder1_count == 2: return ret - remainder1 else: return 0 return 0 return 0
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
def maxsum(a, k): dp = [(float("-inf") if a[0] % k != x else a[0]) for x in range(k)] for i in range(1, len(a)): new = [(0) for x in range(k)] for j in range(k): rem = a[i] % k index = (k - rem + j) % k new[j] = max( dp[j], dp[index] + a[i], a[i] if a[i] % k == j else float("-inf") ) dp = new return 0 if dp[0] == float("-inf") else dp[0] class Solution: def maxSumDivThree(self, nums: List[int]) -> int: return maxsum(nums, 3)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR STRING VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: max_sums = [0] * 3 for num in nums: for max_sum in max_sums[:]: max_sums[(num + max_sum) % 3] = max( max_sum + num, max_sums[(num + max_sum) % 3] ) return max_sums[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: nums = sorted(nums) s = sum(nums) if s % 3 == 0: return s one = [x for x in nums if x % 3 == 1] two = [x for x in nums if x % 3 == 2] three = [x for x in nums if x % 3 == 0] if s % 3 == 1: if one and len(two) > 1: return s - min(one[0], sum(two[0:2])) if one: return s - one[0] return s - sum(two[0:2]) if s % 3 == 2: if len(one) > 1 and two: return s - min(sum(one[0:2]), two[0]) if len(one) > 1: return s - sum(one[0:2]) return s - two[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER IF VAR RETURN BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dic = [0, 0, 0] for i in nums: if i % 3 == 0: dic[0] += i if dic[2] != 0: dic[2] += i if dic[1] != 0: dic[1] += i elif i % 3 == 1: a, b, c = dic[0], dic[1], dic[2] dic[1] = max(a + i, dic[1]) if b != 0: dic[2] = max(b + i, dic[2]) if c != 0: dic[0] = max(c + i, dic[0]) elif i % 3 == 2: a, b, c = dic[0], dic[1], dic[2] dic[2] = max(a + i, dic[2]) if c != 0: dic[1] = max(c + i, dic[1]) if b != 0: dic[0] = max(b + i, dic[0]) return dic[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums): total = sum(nums) mod_total = total % 3 if mod_total == 0: return total else: def min_n(nums, n): if len(nums) < n: return 10**6 sum_n = 0 for i in range(n): min_current = min(nums) min_current_ind = nums.index(min_current) sum_n += nums.pop(min_current_ind) return sum_n mod_1 = [e for e in nums if e % 3 == 1] mod_2 = [e for e in nums if e % 3 == 2] if mod_total == 1: one_mod_1 = min_n(mod_1, 1) two_mod_2 = min_n(mod_2, 2) return total - min(one_mod_1, two_mod_2) elif mod_total == 2: two_mod_1 = min_n(mod_1, 2) one_mod_2 = min_n(mod_2, 1) return total - min(two_mod_1, one_mod_2)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) dp = [0, float("-inf"), float("-inf")] for i in range(1, n + 1): x = nums[i - 1] dp1 = [0] * 3 if x % 3 == 0: for j in range(3): dp1[j] = dp[j] + x elif x % 3 == 1: dp1[0] = max(dp[0], dp[2] + x) dp1[1] = max(dp[1], dp[0] + x) dp1[2] = max(dp[2], dp[1] + x) elif x % 3 == 2: dp1[0] = max(dp[0], dp[1] + x) dp1[1] = max(dp[1], dp[2] + x) dp1[2] = max(dp[2], dp[0] + x) dp = dp1 return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: l = [0] * 3 for i in nums: t = [0] * 3 for j in l: t[(j + i) % 3] = max(t[(j + i) % 3], j + i) for g in range(3): l[g] = max(t[g], l[g]) return l[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: nr = len(nums) dp = [([0] * 3) for _ in range(nr)] dp[0][nums[0] % 3] = nums[0] for i in range(1, nr): for j in range(3): dp[i][j] = dp[i - 1][j] for j in range(3): new_num = dp[i - 1][j] + nums[i] new_rem = new_num % 3 if dp[i][new_rem] < new_num: dp[i][new_rem] = new_num return dp[nr - 1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, float("-inf"), float("-inf")] for n in nums: dp = [max(dp[i], dp[(3 - n % 3 + i) % 3] + n) for i in range(3)] return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [([-float("inf")] * len(nums)) for _ in range(3)] for i, n in enumerate(nums): if i == 0: dp[n % 3][0] = n else: for rem in range(3): dp[(n + rem) % 3][i] = max( dp[rem][i - 1] + n, dp[(n + rem) % 3][i - 1] ) dp[n % 3][i] = max(n, dp[n % 3][i]) return dp[0][-1] if dp[0][-1] != -float("inf") else 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: ans = 0 next_nums = [] nums1, nums2 = [], [] nums.sort(reverse=True) for n in nums: val = n % 3 if val == 0: ans += n elif val == 1: nums1.append(n) else: nums2.append(n) if len(nums1) < len(nums2): small = nums1 big = nums2 else: small = nums2 big = nums1 mod = (len(big) - len(small)) % 3 val1, val2 = 0, 0 if mod == 0: val1 = sum(big) + sum(small) else: val1 = sum(small) + sum(big[:-mod]) if len(big) >= 3 and 3 - mod <= len(small): val2 = sum(small[: -(3 - mod)]) + sum(big) return ans + max(val1, val2) def traversal(self, nums1, nums2, idx1, idx2, curr_val, visited): if idx1 == len(nums1) and idx2 == len(nums2): return curr_val, True if (idx1, idx2, curr_val) in visited: return visited[idx1, idx2, curr_val], False if idx1 + 1 <= len(nums1) and idx2 + 1 <= len(nums2): val, ret = self.traversal( nums1, nums2, idx1 + 1, idx2 + 1, curr_val + nums1[idx1] + nums2[idx2], visited, ) if ret: return val, True max_val = max(max_val, val) max_val = curr_val if idx2 + 3 <= len(nums2): s = nums2[idx2] + nums2[idx2 + 1] + nums2[idx2 + 2] val, ret = self.traversal( nums1, nums2, idx1, idx2 + 3, curr_val + s, visited ) if ret: return val, True max_val = max(max_val, val) if idx1 + 3 <= len(nums1): s = nums1[idx1] + nums1[idx1 + 1] + nums1[idx1 + 2] val, ret = self.traversal( nums1, nums2, idx1 + 3, idx2, curr_val + s, visited ) if ret: return val, True max_val = max(max_val, val) visited[idx1, idx2, curr_val] = max_val return max_val, True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR NUMBER
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: @lru_cache(None) def dfs(index, remainder): if index == len(nums): if remainder == 0: return 0 else: return -float("inf") new_remainder = (nums[index] + remainder) % 3 choose = dfs(index + 1, new_remainder) + nums[index] no_choose = dfs(index + 1, remainder) return max(choose, no_choose) return dfs(0, 0)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0] * 3 for num in nums: r = num % 3 pre = dp[:] for i in range(3): if pre[i]: dp[(i + r) % 3] = max(pre[(i + r) % 3], pre[i] + num) if dp[r] == 0: dp[r] = num return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: snum = sum(nums) if not snum % 3: return snum ones, twos = [], [] nums.sort() for k, v in enumerate(nums): if v % 3 == 1: ones += [nums[k]] if v % 3 == 2: twos += [nums[k]] if snum % 3 == 1: snum = ( max(snum - ones[0], snum - sum(twos[:2])) if len(twos) >= 2 else snum - ones[0] ) if snum % 3 == 2: snum = ( max(snum - twos[0], snum - sum(ones[:2])) if len(ones) >= 2 else snum - twos[0] ) return snum
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums): N = len(nums) dp = [([0] * 3) for _ in range(2)] dp[0][nums[0] % 3] = nums[0] for n in range(1, N): row = n % 2 alt = 1 - row r = nums[n] % 3 for i in range(3): if dp[alt][i] > 0: dp[row][(i + r) % 3] = dp[alt][i] + nums[n] for i in range(3): dp[row][i] = max(dp[row][i], dp[alt][i]) dp[row][r] = max(dp[row][r], nums[n]) return dp[1 - N % 2][0]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, 0, 0] for num in nums: newlist = dp.copy() for i in range(len(dp)): curr = newlist[i] + num dp[curr % 3] = max(dp[curr % 3], curr) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: if not nums: return 0 if len(nums) == 1 and nums[0] % 3: return 0 sum_ = sum(nums) mod_ = sum_ % 3 if not mod_: return sum_ vals = { (False): self.min_two([n for n in nums if n % 3 == 3 - mod_]), (True): [min([n for n in nums if n % 3 == mod_])], } if vals[True] and (len(vals[False]) <= 1 or vals[True][0] < sum(vals[False])): return sum_ - vals[True][0] elif len(vals[False]) == 2: return sum_ - sum(vals[False]) return 0 def min_two(self, l): if not l: return [] idx = [i for i in range(len(l))] out = [min(idx, key=lambda i: l[i])] idx.pop(out[0]) if idx: out.append(min(idx, key=lambda i: l[i])) return [l[i] for i in out]
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR RETURN VAR ASSIGN VAR DICT NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR FUNC_DEF IF VAR RETURN LIST ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: sumVal = sum(nums) if sumVal % 3 == 0: return sumVal categories = [[], [], []] for num in nums: categories[num % 3].append(num) categories[1].sort() categories[2].sort() if sumVal % 3 == 1: if len(categories[2]) < 2: return sumVal - categories[1][0] elif not categories[1]: return sumVal - categories[2][0] - categories[2][1] else: remainder = min(categories[1][0], categories[2][0] + categories[2][1]) return sumVal - remainder elif len(categories[1]) < 2: return sumVal - categories[2][0] elif not categories[2]: return sumVal - categories[1][0] - categories[1][1] else: remainder = min(categories[2][0], categories[1][0] + categories[1][1]) return sumVal - remainder
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR LIST LIST LIST LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, A): print(A) seen = [0, 0, 0] for a in A: temp = seen.copy() for i in seen: temp[(i + a) % 3] = max(temp[(i + a) % 3], i + a) seen = temp return seen[0]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [([0] * 3) for i in range(len(nums))] for i in range(len(dp)): dp[i] = list(dp[i - 1]) for k in range(3): index = ((dp[i - 1][k] if i - 1 >= 0 else 0) + nums[i]) % 3 dp[i][index] = max( (dp[i - 1][k] if i - 1 >= 0 else 0) + nums[i], dp[i][index] ) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [[0, 0, 0]] * (len(nums) + 1) for i in range(len(nums)): cand = dp[i] + [(n + nums[i]) for n in dp[i]] for c in cand: j = c % 3 dp[i + 1][j] = max(dp[i + 1][j], c) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: if sum(nums) % 3 == 0: return sum(nums) mod1 = [num for num in nums if num % 3 == 1] mod2 = [num for num in nums if num % 3 == 2] mod1_min = min(mod1 or [float("inf")]) mod2_min = min(mod2 or [float("inf")]) if mod1_min != float("inf"): mod1.remove(mod1_min) if mod2_min != float("inf"): mod2.remove(mod2_min) possibilities = [ sum(nums) - mod1_min, sum(nums) - mod2_min, sum(nums) - mod1_min - min(mod1 or [float("inf")]), sum(nums) - mod2_min - min(mod2 or [float("inf")]), ] return max([pos for pos in possibilities if pos % 3 == 0] or [0])
CLASS_DEF FUNC_DEF VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR STRING BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER LIST NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) dp = [([0] * 3) for _ in range(n + 1)] dp[0][1] = float("-inf") dp[0][2] = float("-inf") print(dp) for i in range(1, n + 1): if nums[i - 1] % 3 == 0: dp[i][0] = max(dp[i - 1][0], dp[i - 1][0] + nums[i - 1]) dp[i][1] = max(dp[i - 1][1], dp[i - 1][1] + nums[i - 1]) dp[i][2] = max(dp[i - 1][2], dp[i - 1][2] + nums[i - 1]) elif nums[i - 1] % 3 == 1: dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] + nums[i - 1]) dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + nums[i - 1]) dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + nums[i - 1]) else: dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + nums[i - 1]) dp[i][1] = max(dp[i - 1][1], dp[i - 1][2] + nums[i - 1]) dp[i][2] = max(dp[i - 1][2], dp[i - 1][0] + nums[i - 1]) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: maxes = [0, 0, 0, 0] for num in nums: new_maxes = [x for x in maxes] for module in maxes: idx = (num + module) % 3 new_maxes[idx] = max(new_maxes[idx], num + module) maxes = new_maxes return maxes[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: memo = {} self.helper(0, nums, memo) return memo[0, 0] def helper(self, index, nums, memo): if index == len(nums): memo[index, 0] = 0 memo[index, 1] = 0 memo[index, 2] = 0 else: self.helper(index + 1, nums, memo) for i in range(3): if i != 0 and memo[index + 1, i] == 0: memo[index, (i + nums[index]) % 3] = max( 0, memo[index + 1, (i + nums[index]) % 3] ) else: new_mod = (nums[index] + memo[index + 1, i]) % 3 new_sum = max( nums[index] + memo[index + 1, i], memo[index + 1, new_mod] ) memo[index, new_mod] = new_sum return
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR NUMBER NUMBER VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: S = sum(nums) if S % 3 == 0: return S ns1 = sorted([n for n in nums if n % 3 == 1]) ns2 = sorted([n for n in nums if n % 3 == 2]) remove = S if [[], ns1, ns2][S % 3]: remove = min(remove, [[], ns1, ns2][S % 3][0]) if len([[], ns1, ns2][3 - S % 3]) > 1: remove = min(remove, sum([[], ns1, ns2][3 - S % 3][:2])) return S - remove
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF LIST LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST LIST VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR LIST LIST VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR LIST LIST VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dic = {(0): 0} for n in nums: dic_new = {} for i, s in dic.items(): r = (i + n) % 3 dic_new[r] = max(n + s, dic.get(r, 0)) for i, s in dic_new.items(): dic[i] = max(s, dic.get(i, 0)) return dic.get(0, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) if n == 0: return 0 dp = [0, 0, 0] for num in nums: for i in dp[:]: dp[(i + num) % 3] = max(dp[(i + num) % 3], i + num) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) dp = [[(0) for j in range(3)] for i in range(n)] dp[0][nums[0] % 3] = nums[0] for i in range(1, n): for j in range(3): dp[i][j] = dp[i - 1][j] if (j - nums[i]) % 3 == 0: dp[i][j] = max(dp[i - 1][(j - nums[i]) % 3] + nums[i], dp[i - 1][j]) elif dp[i - 1][(j - nums[i]) % 3] > 0: dp[i][j] = max(dp[i - 1][(j - nums[i]) % 3] + nums[i], dp[i - 1][j]) return dp[n - 1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0] * 3 for num in nums: tmp = list(dp) for s in tmp: dp[(s + num) % 3] = max([dp[(s + num) % 3], s + num]) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR LIST VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: m0 = 0 m1 = 0 m2 = 0 for n in nums: m = n % 3 m0n, m1n, m2n = m0, m1, m2 if m == 0: m0n = m0 + n m1n = m1 + n m2n = m2 + n elif m == 1: if m2 % 3 == 2: m0n = max(m0, m2 + n) m1n = max(m1, m0 + n) if m1 % 3 == 1: m2n = max(m2, m1 + n) elif m == 2: if m1 % 3 == 1: m0n = max(m0, m1 + n) if m2 % 3 == 2: m1n = max(m1, m2 + n) m2n = max(m2, m0 + n) m0, m1, m2 = m0n, m1n, m2n return m0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: total = sum(nums) nums.sort() rest = total % 3 if not rest: return total a, b = 0, 0 for n in nums: if b and n > b: break mod = n % 3 if mod: if rest == mod: return total - n elif not a: a = n elif not b: b = a + n return total - b
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR IF VAR VAR RETURN BIN_OP VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, None, None] for num in nums: mod = num % 3 if mod == 0: dp[0], dp[1], dp[2] = ( dp[0] + num, dp[1] + num if dp[1] else None, dp[2] + num if dp[2] else None, ) elif mod == 1: dp[0], dp[1], dp[2] = ( max(dp[0], dp[2] + num if dp[2] else 0), max(dp[1] if dp[1] else 0, dp[0] + num), ( max(dp[2] if dp[2] else 0, dp[1] + num if dp[1] else 0) if dp[1] or dp[2] else None ), ) elif mod == 2: dp[0], dp[1], dp[2] = ( max(dp[0], dp[1] + num if dp[1] else 0), ( max(dp[1] if dp[1] else 0, dp[2] + num if dp[2] else 0) if dp[1] or dp[2] else None ), max(dp[2] if dp[2] else 0, dp[0] + num), ) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NONE NONE FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR NONE VAR NUMBER BIN_OP VAR NUMBER VAR NONE IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NONE IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NONE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: mod = [0, -1, -1] mod[nums[0] % 3] = nums[0] for i in range(1, len(nums)): mod2 = list(mod) for j in range(3): if mod[j] != -1: newSum = mod[j] + nums[i] mod2[newSum % 3] = max(mod[newSum % 3], newSum) mod = mod2 return mod[0] if mod[0] != -1 else 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: if not nums: return 0 dp = [[(0) for _ in range(3)] for _ in range(len(nums))] dp[0][nums[0] % 3] = nums[0] for i in range(1, len(nums)): for j in range(3): dp[i][j] = dp[i - 1][j] currNum = nums[i] for j in range(3): curr = dp[i - 1][j] + nums[i] k = curr % 3 dp[i][k] = max(dp[i][k], curr) return dp[len(nums) - 1][0]
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: @lru_cache(None) def recurse(i, pos): if pos < 0: if i == 0: return 0 return -10000000 temp = max( nums[pos] + recurse((i + nums[pos]) % 3, pos - 1), recurse(i % 3, pos - 1), ) return temp return recurse(0, len(nums) - 1)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) if n == 0: return 0 dp = [([0] * 3) for i in range(n)] dp[0][nums[0] % 3] = nums[0] for i in range(1, n): t1 = nums[i] % 3 dp[i][0] = ( max(dp[i - 1][(3 - t1) % 3] + nums[i], dp[i - 1][0]) if dp[i - 1][(3 - t1) % 3] != 0 else dp[i - 1][0] ) dp[i][1] = ( max(dp[i - 1][(3 - t1 + 1) % 3] + nums[i], dp[i - 1][1]) if dp[i - 1][(3 - t1 + 1) % 3] != 0 else dp[i - 1][1] ) dp[i][2] = ( max(dp[i - 1][(3 - t1 + 2) % 3] + nums[i], dp[i - 1][2]) if dp[i - 1][(3 - t1 + 2) % 3] != 0 else dp[i - 1][2] ) dp[i][t1] = max(nums[i], dp[i][t1]) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) s = sum(nums) if s % 3 == 0: return s pos_1_1, pos_2_1 = True, True min_1_1, min_1_2, min_2_1, min_2_2 = ( float("inf"), float("inf"), float("inf"), float("inf"), ) for i in range(n): if nums[i] % 3 == 1: if nums[i] < min_1_1: min_1_1 = nums[i] pos_1_1 = i elif nums[i] % 3 == 2: if nums[i] < min_2_1: min_2_1 = nums[i] pos_2_1 = i for i in range(n): if i == pos_1_1 or i == pos_2_1: continue if nums[i] % 3 == 1: min_1_2 = min(min_1_2, nums[i]) if nums[i] % 3 == 2: min_2_2 = min(min_2_2, nums[i]) if s % 3 == 1: return s - min(min_1_1, min_2_1 + min_2_2) return s - min(min_2_1, min_1_1 + min_1_2)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) dp = [[(0) for i in range(3)] for i in range(n + 1)] dp[0] = [0, -1e18, -1e18] for i, v in enumerate(nums): for k in range(3): dp[i + 1][k] = max(dp[i][k], v + dp[i][(k - v % 3 + 3) % 3]) return dp[n][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: n = len(nums) dp = [([0] * 3) for _ in range(n)] for i, x in enumerate(nums): m = x % 3 if i > 0: dp[i] = dp[i - 1][:] rot = [dp[i][(x - m) % 3] for x in range(3)] for j in range(3): if rot[j] > 0: dp[i][j] = max(dp[i][j], rot[j] + x) dp[i][m] = max(dp[i][m], x) print(dp) return dp[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [([-math.inf] * 3) for _ in nums] dp[0][nums[0] % 3] = nums[0] for i, n in enumerate(nums[1:], start=1): dp[i][n % 3] = n for j in range(3): dp[i][j] = max(dp[i][j], dp[i - 1][j], n + dp[i - 1][(j - n % 3) % 3]) ans = dp[-1][0] return ans if ans > 0 else 0
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR NUMBER VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [(0) for i in range(3)] for num in nums: tmp = dp[:] for remainder in range(3): dp[(tmp[remainder] + num) % 3] = max( dp[(tmp[remainder] + num) % 3], tmp[remainder] + num ) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR RETURN VAR NUMBER VAR
Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.   Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3). Example 2: Input: nums = [4] Output: 0 Explanation: Since 4 is not divisible by 3, do not pick any number. Example 3: Input: nums = [1,2,3,4,4] Output: 12 Explanation: Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).   Constraints: 1 <= nums.length <= 4 * 10^4 1 <= nums[i] <= 10^4
class Solution: def maxSumDivThree(self, nums: List[int]) -> int: dp = [0, 0, 0] for i in range(len(nums)): for j in dp[:]: dp[(j + nums[i]) % 3] = max(j + nums[i], dp[(j + nums[i]) % 3]) return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR NUMBER VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, a: List[int]) -> int: n = len(a) ans = 0 neg = -1 zero = -1 cnt = 0 for i, x in enumerate(a): if x == 0: neg = zero = i cnt = 0 elif x < 0: if neg == zero: neg = i cnt += 1 if cnt % 2 == 0: ans = max(ans, i - zero) else: ans = max(ans, i - neg) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: N = len(nums) ans = 0 i = j = 0 while i < N: if nums[i] == 0: i += 1 continue j = i while j < N and nums[j]: j += 1 negs = 0 firstneg = lastneg = None for k in range(i, j): if nums[k] < 0: negs += 1 if firstneg is None: firstneg = k lastneg = k if negs % 2 == 0: ans = max(ans, j - i) else: ans = max(ans, firstneg - i, j - i - 1 - (firstneg - i)) ans = max(ans, lastneg - i, j - i - 1 - (lastneg - i)) i = j return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NONE FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
def f(data): n2 = len(data) f0 = -1 f1 = -1 n = 0 for ind, d in enumerate(data): if d == 1: continue n += 1 if f0 == -1: f0 = ind f1 = ind if n & 1 == 0: return n2 return max(f1, n2 - f0 - 1) class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 prev = -1 data = [] for ind, num in enumerate(nums): if num == 0: if data: res = max(res, f(data)) prev = ind data = [] continue data.append(1 if num > 0 else -1) res = max(res, f(data)) return res
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: m = 0 neg = 0 pos = 0 positiveBeforeFirstNeg = 0 pos2 = 0 for i in nums: if i == 0: if neg > 0 and neg % 2 != 0: neg -= 1 if positiveBeforeFirstNeg < pos2 and neg > 2: pos -= positiveBeforeFirstNeg pos += pos2 m = max(max(pos, pos2) + neg, m) neg, pos, pos2 = 0, 0, 0 elif i < 0: if neg == 0: positiveBeforeFirstNeg = pos neg += 1 if neg % 2 == 0: pos += pos2 pos2 = 0 elif neg % 2 != 0: pos2 += 1 else: pos += 1 if positiveBeforeFirstNeg < pos2 and neg > 2: pos -= positiveBeforeFirstNeg pos += pos2 if neg > 0 and neg % 2 != 0: neg -= 1 m = max(max(pos, pos2) + neg, m) return m
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: p = 0 n = 0 longest_p = 0 for num in nums: longest_p = max(longest_p, p) if num == 0: p = 0 n = 0 elif num > 0: prev_p = p prev_n = n p = prev_p + 1 if prev_n > 0: n = prev_n + 1 else: n = 0 elif num < 0: prev_p = p prev_n = n n = prev_p + 1 if prev_n > 0: p = prev_n + 1 else: p = 0 longest_p = max(longest_p, p) return longest_p
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: i = 0 negatives = [-1, -1] p = 1 ans = 0 for j, n in enumerate(nums): if not n: p = 1 i = j + 1 negatives = [-1, -1] else: p *= n if n < 0: if negatives[0] < 0: negatives[0] = j negatives[1] = j if p > 0: ans = max(ans, j - i + 1) else: ans = max(ans, negatives[1] - i, j - negatives[0]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) dp = [([0] * (n + 1)) for i in range(2)] for i, x in enumerate(nums): if x > 0: dp[0][i + 1] = dp[0][i] + 1 if dp[1][i] > 0: dp[1][i + 1] = dp[1][i] + 1 elif x < 0: if dp[1][i] > 0: dp[0][i + 1] = dp[1][i] + 1 dp[1][i + 1] = dp[0][i] + 1 else: dp[0][i + 1] = dp[1][i + 1] = 0 return max(dp[0])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: start, curnegcnt, res, fnegpos, lnegpos = 0, 0, 0, 0, 0 flag = True i, j = 0, len(nums) while i < j: if nums[i] == 0: flag = True i += 1 start = i curnegcnt = 0 continue if nums[i] < 0: if flag: flag = False fnegpos = i lnegpos = i curnegcnt += 1 if curnegcnt == 0 or curnegcnt % 2 == 0: res = max(res, i - start + 1) else: res = max(res, lnegpos - start, i - fnegpos) i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) pos, neg = [0] * n, [0] * n if nums[0] > 0: pos[0] = 1 if nums[0] < 0: neg[0] = 1 ans = pos[0] for i in range(1, n): if nums[i] > 0: pos[i] = 1 + pos[i - 1] neg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 elif nums[i] < 0: pos[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0 neg[i] = 1 + pos[i - 1] ans = max(ans, pos[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums): n = len(nums) pos, neg = 0, 0 res = 0 for i in range(n): if nums[i] > 0: pos, neg = 1 + pos, 1 + neg if neg else 0 elif nums[i] < 0: pos, neg = 1 + neg if neg else 0, 1 + pos else: pos, neg = 0, 0 res = max(res, pos) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: prod = 1 pos, neg = -1, None ans = 0 for i in range(len(nums)): if nums[i] == 0: pos, neg = i, None prod = 1 else: prod *= nums[i] if prod > 0: ans = max(ans, i - pos) else: if neg == None: neg = i ans = max(ans, i - neg) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR NONE ASSIGN VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: if not nums: return 0 cur_pos_product_len = 1 if nums[0] > 0 else 0 cur_neg_product_len = 1 if nums[0] < 0 else 0 max_len = cur_pos_product_len for i in range(1, len(nums)): if nums[i] > 0: cur_pos_product_len += 1 cur_neg_product_len = ( cur_neg_product_len + 1 if cur_neg_product_len > 0 else 0 ) elif nums[i] < 0: temp = cur_pos_product_len cur_pos_product_len = ( cur_neg_product_len + 1 if cur_neg_product_len > 0 else 0 ) cur_neg_product_len = temp + 1 else: cur_pos_product_len = 0 cur_neg_product_len = 0 max_len = max(max_len, cur_pos_product_len) return max_len
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 dp_po, dp_ne = [0] * (len(nums) + 1), [0] * (len(nums) + 1) for i, value in enumerate(nums): if value == 0: dp_po[i + 1] = 0 dp_ne[i + 1] = 0 elif value > 0: dp_po[i + 1] = dp_po[i] + 1 if dp_ne[i] > 0: dp_ne[i + 1] = dp_ne[i] + 1 else: dp_ne[i + 1] = dp_po[i] + 1 if dp_ne[i] > 0: dp_po[i + 1] = dp_ne[i] + 1 if dp_po[i + 1] > res: res = dp_po[i + 1] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = 0 pos, neg = set(), set() for num in nums: if num > 0: pos = {(x + 1) for x in pos} | {1} neg = {(y + 1) for y in neg} elif num < 0: pos = {(x + 1) for x in pos} | {1} neg = {(y + 1) for y in neg} pos, neg = neg, pos else: pos, neg = set(), set() if len(pos): res = max(res, max(pos)) if len(pos): pos = set([max(pos)]) if len(neg): neg = set([max(neg)]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: ret = 0 pos, neg = -1, None curr = 0 for i, n in enumerate(nums): if n == 0: pos, neg = i, None curr = 0 else: if n < 0: curr = 1 - curr if curr == 0: ret = max(ret, i - pos) if curr == 1: if neg is not None: ret = max(ret, i - neg) else: neg = i return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NONE ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NONE ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: accLast = 1 accCurr = 1 lastpos = 0 lastnegative = -1 res = 0 dic = {} for i in range(len(nums)): accCurr = accLast * nums[i] if accCurr == 0: lastpos = i + 1 lastnegative = -1 accCurr = 1 elif accCurr > 0: if lastpos != -1: res = max(res, i + 1 - lastpos) elif lastnegative != -1: res = max(res, i + 1 - lastnegative) else: lastnegative = i + 1 accLast = accCurr return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: maxl = 0 posi = None start = -1 cur = True for i, n in enumerate(nums): if n == 0: start = i posi = None cur = True elif n > 0: if cur: maxl = max(maxl, i - start) elif posi == None: maxl = max(maxl, 1) posi = i else: maxl = max(maxl, i - posi + 1) elif not cur: maxl = max(maxl, i - start) if not posi: posi = i cur = True else: cur = False if posi: maxl = max(maxl, i - posi + 1) return maxl
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: maxl = 0 pos = 0 neg = 0 for n in nums: if n == 0: pos, neg = 0, 0 elif n > 0: pos += 1 neg = neg + 1 if neg != 0 else 0 elif n < 0: oldneg = neg neg = pos + 1 pos = oldneg + 1 if oldneg > 0 else 0 maxl = max(maxl, pos) return maxl
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def sgn(x): if x < 0: return -1 if x == 0: return 0 return 1 nums = list(map(sgn, nums)) def get_ans(start, end): arr = nums[start:end] negative = arr.count(-1) result = end - start if negative & 1 ^ 1: return result return result - min(arr.index(-1), arr[::-1].index(-1)) - 1 nums.append(0) size = len(nums) pair = [0] ans = 0 for i in range(size): if nums[i] == 0: if not pair: continue pair.append(i) ans = max(ans, get_ans(*pair)) pair = [i + 1] return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def _getMaxLen(self, nums: List[int]) -> int: lz = [i for i, v in enumerate(nums) if v < 0] ts = len(nums) if len(lz) % 2 == 0: return ts if len(lz) == 1: lz = [lz[0], lz[0]] ls = lz[-1] rs = ts - (lz[0] + 1) if ls > rs: return ls return rs def getMaxLen(self, nums: List[int]) -> int: r = 0 zero1 = 0 try: zero1 = nums.index(0) r = self._getMaxLen(nums[:zero1]) nums = nums[zero1 + 1 :] while True: zero2 = nums.index(0) r2 = self._getMaxLen(nums[:zero2]) if r2 > r: r = r2 zero1 = zero2 nums = nums[zero1 + 1 :] except ValueError: pass r2 = self._getMaxLen(nums) if r2 > r: r = r2 return r
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: pos_l = 0 neg_l = 0 best = 0 for n in nums: if n == 0: pos_l = 0 neg_l = 0 elif n > 0: pos_l += 1 if neg_l > 0: neg_l += 1 elif neg_l == 0 and pos_l == 0: neg_l = 1 elif neg_l > 0 and pos_l > 0: pos_l, neg_l = neg_l + 1, pos_l + 1 elif neg_l > 0: pos_l = neg_l + 1 neg_l = 1 elif pos_l > 0: neg_l = pos_l + 1 pos_l = 0 best = max(best, pos_l) return best
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def getMaxLenHelper(nums: List[int]) -> int: ans = 0 count, count1, negativeCount = 0, 0, 0 for i in range(len(nums)): if nums[i] == 0: if count > ans: ans = count count, count1, negativeCount = 0, 0, 0 else: if nums[i] > 0: count += 1 count1 += 1 if nums[i] < 0: negativeCount += 1 if negativeCount == 2: count = count1 negativeCount = 0 else: if count > ans: ans = count count = 0 if count > ans: ans = count return ans return max(getMaxLenHelper(nums), getMaxLenHelper(nums[::-1]))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [([0] * 2) for _ in range(len(nums))] if nums[0] < 0: dp[0][1] = 1 if nums[0] > 0: dp[0][0] = 1 res = dp[0][0] for i, num in enumerate(nums): if i and num < 0: dp[i][1] = dp[i - 1][0] + 1 if dp[i - 1][1]: dp[i][0] = dp[i - 1][1] + 1 if i and num > 0: dp[i][0] = dp[i - 1][0] + 1 if dp[i - 1][1]: dp[i][1] = dp[i - 1][1] + 1 res = max(res, dp[i][0]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: res = curr = 0 dp = {(0): 0} for i, num in enumerate(nums, 1): if num == 0: curr = 0 dp = {(0): i} continue curr = (curr + (num < 0)) % 2 if curr not in dp: dp[curr] = i else: res = max(res, i - dp[curr]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: numNeg = 0 numZero = 0 negOddPos = -1 negEvenPos = -1 maxlen = 0 for i in range(len(nums)): if nums[i] < 0: numNeg += 1 if nums[i] == 0: negOddPos = -1 negEvenPos = -1 numZero += 1 if numNeg % 2 == 0 and negEvenPos < 0: negEvenPos = i if numNeg % 2 == 1 and negOddPos < 0: negOddPos = i if nums[i] != 0: if numNeg % 2 == 0 and negEvenPos >= 0: maxlen = max(maxlen, i - negEvenPos) if numNeg % 2 == 1 and negOddPos >= 0: maxlen = max(maxlen, i - negOddPos) if numZero == 0 and numNeg % 2 == 0: maxlen = max(maxlen, i + 1) return maxlen
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) dp = [[0, 0] for i in range(n)] if nums[0] > 0: dp[0][0] = 1 elif nums[0] < 0: dp[0][1] = 1 for i in range(1, n): if nums[i] == 0: continue elif nums[i] > 0: if dp[i - 1][1] > 0: dp[i][1] = dp[i - 1][1] + 1 dp[i][0] = dp[i - 1][0] + 1 else: if dp[i - 1][1] > 0: dp[i][0] = dp[i - 1][1] + 1 dp[i][1] = dp[i - 1][0] + 1 maxLen = 0 for i in range(n): maxLen = max(maxLen, dp[i][0]) return maxLen
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [(0, 0)] if nums[0] < 0: dp[0] = 0, 1 elif nums[0] > 0: dp[0] = 1, 0 for i in range(1, len(nums)): if nums[i] == 0: dp.append((0, 0)) elif nums[i] > 0: if nums[i - 1] != 0: if dp[i - 1][1] != 0: dp.append((dp[i - 1][0] + 1, dp[i - 1][1] + 1)) else: dp.append((dp[i - 1][0] + 1, 0)) else: dp.append((1, 0)) elif nums[i] < 0: if nums[i - 1] != 0: if dp[i - 1][1] != 0: dp.append((dp[i - 1][1] + 1, dp[i - 1][0] + 1)) else: dp.append((0, dp[i - 1][0] + 1)) else: dp.append((0, 1)) positives = [dp[i][0] for i in range(len(nums))] return max(positives) maxProd = 0 for i in range(len(nums)): if nums[i] > 0: maxProd = 1 prods = [nums[0]] zeroFound = [False] * len(nums) if nums[0] == 0: zeroFound[0] = True for i in range(1, len(nums)): if zeroFound[i - 1] or nums[i] == 0: zeroFound[i] = True mostRecentPostZeroNeg = [float("-inf")] * len(nums) if nums[0] < 0: mostRecentPostZeroNeg[0] = 0 for i in range(1, len(nums)): if nums[i] == 0: continue if nums[i] > 0: mostRecentPostZeroNeg[i] = mostRecentPostZeroNeg[i - 1] if nums[i] < 0: if mostRecentPostZeroNeg[i - 1] == float("-inf"): mostRecentPostZeroNeg[i] = i else: mostRecentPostZeroNeg[i] = mostRecentPostZeroNeg[i - 1] for i in range(1, len(nums)): if prods[i - 1] == 0: if nums[i] > 0: prods.append(1) elif nums[i] < 0: prods.append(-1) else: prods.append(0) elif prods[i - 1] < 0: if nums[i] < 0: prods.append(1) elif nums[i] > 0: prods.append(-1) else: prods.append(0) elif prods[i - 1] > 0: if nums[i] < 0: prods.append(-1) elif nums[i] > 0: prods.append(1) else: prods.append(0) dp = [] if nums[0] == 0: dp.append(0) if nums[0] < 0: dp.append(1) if nums[0] > 0: dp.append(1) dp1 = [] if nums[0] < 0: dp1.append(0) if nums[0] > 0: dp1.append(0) if nums[0] == 0: dp1.append(0) for i in range(1, len(nums)): if dp1[-1] == 0: if nums[i] < 0: dp1.append(1) else: dp1.append(0) elif nums[i] == 0: dp1.append(0) else: dp1.append(dp1[-1] + 1) for i in range(1, len(nums)): if nums[i] != 0: dp.append(dp[i - 1] + 1) else: dp.append(0) if prods[i] > 0: maxProd = max(maxProd, dp[i]) elif mostRecentPostZeroNeg[i] != float("-inf"): maxProd = max(maxProd, i - mostRecentPostZeroNeg[i]) return maxProd
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: product = 1 count = 0 firstNegative = -1 maxCount = 0 for i in range(len(nums)): if nums[i] < 0: if firstNegative == -1: firstNegative = i if nums[i] == 0: count = 0 product = 1 firstNegative = -1 else: product *= nums[i] count += 1 if product > 0: maxCount = max(maxCount, count) elif firstNegative != -1: maxCount = max(maxCount, i - firstNegative) return maxCount
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: fn = -1 zi = -1 cn = 0 dig = 0 maxdig = 0 for i in range(len(nums)): if nums[i] < 0: cn += 1 if fn == -1: fn = i if nums[i] == 0: fn = -1 cn = 0 zi = i elif cn % 2 == 0: maxdig = max(i - zi, maxdig) else: maxdig = max(i - fn, maxdig) return maxdig
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def conv(n): if n > 0: return 1 if n == 0: return 0 return -1 nums = list(map(conv, nums)) n = len(nums) def check(size): if size == 0: return True if size > n: return False p = 1 lo = hi = 0 while hi < n: if nums[hi] == 0: p = 1 lo = hi = hi + 1 continue p *= nums[hi] if hi - lo + 1 == size: if p > 0: return True p //= nums[lo] lo += 1 hi += 1 return False res = 0 lo, hi = 0, n while lo <= hi: m = (lo + hi) // 2 r1, r2 = check(m), check(m + 1) if r1 or r2: if r1: res = m if r2: res = m + 1 lo = m + 2 else: hi = m - 1 return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def solve(nums): cnt = 0 for n in nums: if n < 0: cnt += 1 if cnt % 2 == 0: return len(nums) for i in range(len(nums)): if nums[i] < 0: first = i break for i in range(len(nums) - 1, -1, -1): if nums[i] < 0: last = i break return max(last, len(nums) - first - 1) l = 0 ans = 0 nums.append(0) for r in range(len(nums)): if nums[r] == 0: cur = solve(nums[l:r]) ans = max(ans, cur) l = r + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def helper(arr): inds = [] for i, num in enumerate(arr): if num < 0: inds.append(i) if len(inds) % 2 == 0: return len(arr) else: first = max(inds[0], len(arr) - inds[0] - 1) second = max(inds[-1], len(arr) - inds[-1] - 1) return max(first, second) temp = [] for i, num in enumerate(nums): if num == 0: temp.append(i) pos = [] if temp: if nums[: temp[0]]: pos.append(nums[: temp[0]]) for i in range(0, len(temp) - 1): pos.append(nums[temp[i] + 1 : temp[i + 1]]) if nums[temp[-1] + 1 :]: pos.append(nums[temp[-1] + 1 :]) else: pos.append(nums) ans = 0 for arr in pos: ans = max(ans, helper(arr)) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: n = len(nums) if n == 1: return 1 if nums[0] > 0 else 0 p1 = p2 = 0 answer = 0 curr_len = 0 neg_indx = deque([]) while p2 < n: num = nums[p2] if num == 0: answer = max(answer, curr_len) curr_len = 0 p2 += 1 p1 = p2 neg_indx = deque([]) elif num > 0: curr_len += 1 answer = max(answer, curr_len) p2 += 1 else: neg_indx.append(p2) j = p2 + 1 found = False while j < n: num2 = nums[j] if num2 <= 0: if num2 < 0: neg_indx.append(j) found = True break j += 1 if found: curr_len += j - p2 + 1 answer = max(answer, curr_len) if j == n - 1: return answer else: p2 = j + 1 else: first_neg = neg_indx.popleft() while p1 <= first_neg: p1 += 1 curr_len -= 1 if p1 > p2: p2 = p1 answer = max(answer, curr_len) curr_len = 0 else: curr_len += 1 p2 += 1 continue return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR LIST IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: maxProducts = {} numLength = len(nums) maxSize = 0 if nums[0] == 0: maxProducts[0] = [0, 0] elif nums[0] > 0: maxProducts[0] = [1, 0] else: maxProducts[0] = [0, 1] maxSize = max(maxSize, maxProducts[0][0]) for i in range(1, numLength): currentNum = nums[i] maxProducts[i] = [0, 0] if currentNum > 0: maxProducts[i][0] = maxProducts[i - 1][0] + 1 maxProducts[i][1] = ( maxProducts[i - 1][1] if maxProducts[i - 1][1] == 0 else maxProducts[i - 1][1] + 1 ) elif currentNum < 0: maxProducts[i][1] = maxProducts[i - 1][0] + 1 maxProducts[i][0] = ( maxProducts[i - 1][1] if maxProducts[i - 1][1] == 0 else maxProducts[i - 1][1] + 1 ) maxSize = max(maxSize, maxProducts[i][0]) return maxSize
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: first_neg = None last_neg = None first_pos = None cum_prod = 1 max_len = 0 last_zero_idx = 0 for idx, val in enumerate(nums): if val == 0: first_neg = None last_neg = None first_pos = None cum_prod = 1 last_zero_idx = idx + 1 else: cum_prod *= val if cum_prod > 0: max_len = max(max_len, idx - last_zero_idx + 1) else: if first_neg is None: first_neg = idx last_neg = idx max_len = max(max_len, last_neg - first_neg) return max_len
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def helper(nums): result = 0 positiveSoFar = 0 negativeSoFar = 0 for i in range(len(nums)): if nums[i] == 0: positiveSoFar = 0 negativeSoFar = 0 elif nums[i] > 0: positiveSoFar += 1 if negativeSoFar > 0: negativeSoFar += 1 elif nums[i] < 0: if negativeSoFar > 0: positiveSoFar = max(negativeSoFar, positiveSoFar) + 1 negativeSoFar = 0 else: negativeSoFar = positiveSoFar + 1 positiveSoFar = 0 result = max(result, positiveSoFar) return result return max(helper(nums), helper(nums[::-1]))
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, A: List[int]) -> int: n = len(A) lastZ = -1 lastN = n cur = 1 res = 0 for i, a in enumerate(A): cur *= a if cur == 0: lastZ = i lastN = n cur = 1 elif cur > 0: res = max(res, i - lastZ) else: lastN = min(lastN, i) res = max(res, i - lastN) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: dp = [[0, 0] for _ in range(len(nums))] res = 0 if nums[0] > 0: dp[0][0] = 1 elif nums[0] < 0: dp[0][1] = 1 res = max(res, dp[0][0]) for idx in range(1, len(nums)): if nums[idx] == 0: dp[idx][0], dp[idx][1] = 0, 0 elif nums[idx] > 0: dp[idx][0] = dp[idx - 1][0] + 1 if dp[idx - 1][1] > 0: dp[idx][1] = dp[idx - 1][1] + 1 res = max(dp[idx][0], res) elif nums[idx] < 0: dp[idx][1] = dp[idx - 1][0] + 1 if dp[idx - 1][1] > 0: dp[idx][0] = dp[idx - 1][1] + 1 res = max(res, dp[idx][0]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive. A subarray of an array is a consecutive sequence of zero or more values taken out of that array. Return the maximum length of a subarray with positive product.   Example 1: Input: nums = [1,-2,-3,4] Output: 4 Explanation: The array nums already has a positive product of 24. Example 2: Input: nums = [0,1,-2,-3,-4] Output: 3 Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6. Notice that we cannot include 0 in the subarray since that'll make the product 0 which is not positive. Example 3: Input: nums = [-1,-2,-3,0,1] Output: 2 Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3]. Example 4: Input: nums = [-1,2] Output: 1 Example 5: Input: nums = [1,2,3,5,-6,4,0,10] Output: 4   Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9
class Solution: def getMaxLen(self, nums: List[int]) -> int: def check(arr) -> int: if not arr: return 0 pos = neg = -1 pre_prod = 1 res = 0 for i, a in enumerate(arr): pre_prod *= a if pre_prod > 0: if pos == -1: pos = i res = i + 1 else: if neg == -1: neg = i res = max(res, i - neg) return res res = i = j = 0 while j < len(nums): while j < len(nums) and nums[j] == 0: j += 1 i = j while j < len(nums) and nums[j] != 0: j += 1 res = max(res, check(nums[i:j])) return res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR