-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.cpp
47 lines (44 loc) · 1.08 KB
/
Solution.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
38
39
40
41
42
43
44
45
46
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class Solution {
public:
int findMaxLength(vector<int>& nums) {
if (nums.size() == 0) return 0;
vector<int> height (nums.size() + 1, 0);
int sum = 0;
unordered_map<int, pair<int, int>> map;
map[0].first = 0;
map[0].second = 0;
for (int i = 0; i < nums.size(); i ++ ) {
sum += nums[i] == 1 ? 1 : -1;
if (map.count(sum) == 0) {
map[sum].first = i + 1;
map[sum].second = 0;
}
else map[sum].second = i + 1;
}
int res = 0;
for (auto &&[x, y] : map) {
if (y.second != 0 and y.second - y.first > res)
res = y.second - y.first;
}
return res;
}
};
int main() {
Solution a;
vector<int> v = {1, 0};
int res = a.findMaxLength(v);
cout << res;
return 0;
}