Leetcode 525 Solution

This article provides solution to leetcode question 525 (contiguous-array)

https://leetcode.com/problems/contiguous-array

Solution

class Solution:
    def findMaxLength(self, nums: List[int]) -> int:
        m = {0:-1}
        s = 0
        ans = 0
        for i, num in enumerate(nums):
            s += 2 * num - 1
            if s in m:
                ans = max(ans, i - m[s])
            else:
                m[s] = i

        return ans