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
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 3083. Existence of a Substring in a String and Its Reverse (0) | 2024.03.17 |
---|---|
[Leetcode] 3079. Find the Sum of Encrypted Integers (0) | 2024.03.16 |
[Leetcode] 238. Product of Array Except Self (0) | 2024.03.15 |
[Leetcode] 930. Binary Subarrays With Sum (1) | 2024.03.14 |
[Leetcode] 2485. Find the Pivot Integer (0) | 2024.03.13 |