Leetcode 1649 Solution

This article provides solution to leetcode question 1649 (maximum-number-of-non-overlapping-subarrays-with-sum-equals-target)

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