Contiguous Array

Problem Id: 525 Difficulty: Medium


Intuition

Solution


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