-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path525-contiguous-array.c
More file actions
67 lines (51 loc) · 1.64 KB
/
Copy path525-contiguous-array.c
File metadata and controls
67 lines (51 loc) · 1.64 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
61
62
63
64
65
66
67
#include <string>
#include <cassert>
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
/*
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.
Example 1:
Input: nums = [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
Example 2:
Input: nums = [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Constraints:
1 <= nums.length <= 105
nums[i] is either 0 or 1.
*/
class Solution {
public:
int findMaxLength(const vector<int>& nums) {
unordered_map<int,int> mp;
int n = nums.size();
int maxl = 0; int sum = 0;
for(int i = 0; i < n; ++i) {
sum += nums[i] ? 1 : -1;
// equial 0 and 1 without holes
if(sum == 0)
maxl = max(maxl, i+1);
// POV: that sum already exist. Difference of current i and prev index of that sum is new length
else if(mp.count(sum))
maxl = max(maxl, i - mp[sum]);
// fix new index presum
else
mp[sum] = i;
}
return maxl;
}
};
int main(int argc, char * argv[]) {
Solution sol;
assert(sol.findMaxLength({0,1}) == 2);
assert(sol.findMaxLength({0,1,0}) == 2);
assert(sol.findMaxLength({0,1,0,1}) == 4);
assert(sol.findMaxLength({0,0,1,1}) == 4);
assert(sol.findMaxLength({0,0,1,0,0,0,1,1}) == 6);
cout << "tests is passed!" << endl;
return 0;
}