525. Contiguous Array
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array, Hash Table, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result, count = 0, 0
lookup = {0: -1}
for i, num in enumerate(nums):
count += 1 if num == 1 else -1
if count in lookup:
result = max(result, i - lookup[count])
else:
lookup[count] = i
return result
Solution from kamyu104/LeetCode-Solutions · MIT