Skip to content

Commit 193b462

Browse files
committed
Time: 108 ms (83.76%), Space: 83.1 MB (77.65%) - LeetHub
1 parent 49411a9 commit 193b462

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

contiguous-array/contiguous-array.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int findMaxLength(vector<int>& nums) {
4+
int n = nums.size();
5+
unordered_map<int,int> mp;
6+
mp[0] = -1;
7+
8+
int pref = 0;
9+
int mx = 0;
10+
for(int i = 0; i < n; i++){
11+
pref += (nums[i] == 0 ? -1 : nums[i]);
12+
if(mp.find(pref) != mp.end()){
13+
mx = max(mx, i - mp[pref]);
14+
}else{
15+
mp[pref] = i;
16+
}
17+
}
18+
19+
return mx;
20+
}
21+
};

0 commit comments

Comments
 (0)