-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3719.cpp
More file actions
43 lines (43 loc) · 941 Bytes
/
3719.cpp
File metadata and controls
43 lines (43 loc) · 941 Bytes
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
class Manager {
private:
unordered_map<int, int> mp;
int odd;
int even;
public:
Manager() {
odd = 0;
even = 0;
}
void insert(int x) {
if (!mp.count(x)) {
if (x & 1) odd++;
else even++;
}
mp[x]++;
}
void erase(int x) {
mp[x]--;
if (mp[x] == 0) {
if (x & 1) odd--;
else even--;
}
}
bool isValid() {
return odd == even && odd >= 1;
}
};
class Solution {
public:
int longestBalanced(vector<int>& nums) {
int n = nums.size();
int res = 0;
for (int left = 0; left < n; ++left) {
Manager* manager = new Manager();
for (int right = left; right < n; ++right) {
manager->insert(nums[right]);
if (manager->isValid()) res = max(res, right - left + 1);
}
}
return res;
}
};