본문 바로가기

IT/코딩테스트

[Leetcode] 525. Contiguous Array

728x90

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

class Solution:
    def findMaxLength(self, nums: List[int]) -> int:
        length = 0

        count = 0
        count_map = {0: -1}
        for i in range(len(nums)):
            if nums[i] == 0:
                count -= 1
            else:
                count += 1
            
            if count in count_map:
                length = max(length, i - count_map[count])
            else:
                count_map[count] = i

        return length
728x90