Leetcode 1649 Solution
This article provides solution to leetcode question 1649 (maximum-number-of-non-overlapping-subarrays-with-sum-equals-target)
Access this page by simply typing in "lcs 1649" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/maximum-number-of-non-overlapping-subarrays-with-sum-equals-target
Solution
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
m = {0}
s = 0
ans = 0
for num in nums:
s += num
if s - target in m:
ans += 1
m = {0}
s = 0
else:
m.add(s)
return ans