/
lt-max-length-binary.cpp
37 lines (34 loc) · 1.04 KB
/
lt-max-length-binary.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
34
35
36
37
class Solution {
public:
int findMaxLength(vector<int>& nums) {
vector<int> arr(2 * nums.size() + 1, -2);
int n = nums.size();
int maxn = 0;
int count = 0;
arr[n] = -1;
for (int i = 0; i < n; i++) {
// ===============
cout << "[";
for(int j = 0; j < arr.size(); j++){
if(j == arr.size() - 1){
cout << arr[j];
}
else {
cout << arr[j] << ",";
}
}
cout << "]" << endl;
/// ==========
count += (nums[i] == 0 ? -1 : 1);
cout << "Count " << count << endl;
if (arr[count + n] >= -1) {
maxn = max(maxn, i - arr[count + n]);
cout << "Maxn " << maxn << endl;
} else {
cout << "arr[" << count << "+" << n << "] = " << i << endl;
arr[count + n] = i;
}
}
return maxn;
}
};