-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path525_ContigousArray.cpp
More file actions
60 lines (54 loc) · 1.22 KB
/
Copy path525_ContigousArray.cpp
File metadata and controls
60 lines (54 loc) · 1.22 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<string>
#include <set>
#include<vector>
#include<iostream>
#include<map>
#include<stack>
#include <unordered_set>
#include <algorithm> // std::min_element, std::max_element
#include<unordered_map>
using namespace std;
int findMaxLength1(vector<int>& nums) {
map<int, int> mp;
mp[0] = -1;
int count = 0;
int maxVal = 0;
for (int i = 0; i < nums.size(); i++)
{
count = count + (nums[i] == 0 ? -1 : 1);
if (mp.find(count) != mp.end())
{
int index = mp[count];
maxVal = max(maxVal, i - mp[count]);
}
else
{
mp[count] = i;
}
}
return maxVal;
}
int findMaxLength(vector<int>& nums) {
unordered_map<int, int> mp;
mp[0] = -1;
int maxlen = 0, count = 0;
for (int i = 0; i < nums.size(); i++) {
count = count + (nums[i] == 1 ? 1 : -1);
if (mp.find(count) != mp.end()) {
maxlen = max(maxlen, i - mp[count]);
}
else {
mp[count] = i;
}
}
return maxlen;
}
int main525()
{
//int count = climbStairs(5);
int N = 2;
vector<int> a{ 0,0,1,0,0,0,1,1};
int lnResult = findMaxLength(a);
cout << lnResult;
return 0;
}