-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path525.cpp
33 lines (33 loc) · 790 Bytes
/
525.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int A[nums.size()];
for(int i=0;i<nums.size();i++){
if(nums[i]==0){
A[i]=-1;
}
else{
A[i]=1;
}
}
int sum=0;
int max_len=0;
map<int,int>mp;
for(int i=0;i<nums.size();i++){
sum+=A[i];
if(max_len==0 && sum==0){
max_len=1;
}
if(sum==0){
max_len=i+1;
}
if(mp.find(sum)!=mp.end()){
max_len=max(max_len,i-mp[sum]);
}
else{
mp[sum]=i;
}
}
return max_len;
}
};