Skip to content

Commit 4dac9d4

Browse files
author
Partho Biswas
committed
525_Contiguous_Array
1 parent 25538c3 commit 4dac9d4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ I have solved quite a number of problems from several topics. See the below tabl
171171
|60| [941. Valid Mountain Array](https://tinyurl.com/tgdqrrk) | [Python](https://tinyurl.com/wu6rdaw/941_Valid_Mountain_Array.py), [Swift](https://tinyurl.com/wuja3c4/941_Valid_Mountain_Array.swift) | | Easy | |
172172
|61| [731. My Calendar II](https://tinyurl.com/sde6smv) | [Python](https://tinyurl.com/wu6rdaw/731_My_Calendar_II.py), [Swift](https://tinyurl.com/wuja3c4/731_My_Calendar_II.swift) | | Medium | Merge interval |
173173
|62| [59. Spiral Matrix II](https://tinyurl.com/pqfjvm7) | [Python](https://tinyurl.com/wu6rdaw/59_Spiral_Matrix_II.py), [Swift](https://tinyurl.com/wuja3c4/59_Spiral_Matrix_II.swift) | | Medium | loved it |
174+
|63| **[525. Contiguous Array](https://tinyurl.com/txbs9wh)** | [Python](https://tinyurl.com/wu6rdaw/525_Contiguous_Array.py), [Swift](https://tinyurl.com/wuja3c4/525_Contiguous_Array.swift) | [Art 1](https://tinyurl.com/qmbd2vl) | Medium | loved it. Check again |
174175

175176

176177
</p>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://tinyurl.com/qmbd2vl
2+
class Solution(object):
3+
def findMaxLength(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: int
7+
"""
8+
count, maxLenSoFar = 0, 0
9+
counterMap = {count: -1} # count:index,
10+
11+
for i in range(len(nums)):
12+
currentNum = nums[i]
13+
if currentNum == 0:
14+
count -= 1
15+
else:
16+
count += 1
17+
18+
if count in counterMap:
19+
maxLenSoFar = max(maxLenSoFar, i - counterMap[count])
20+
else:
21+
counterMap[count] = i
22+
23+
return maxLenSoFar

0 commit comments

Comments
 (0)