-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3411.cpp
More file actions
51 lines (50 loc) · 1.46 KB
/
3411.cpp
File metadata and controls
51 lines (50 loc) · 1.46 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
class Solution {
public:
bool isValid(vector<int>& counts) {
for (int i = 2; i < 11; ++i) {
if (counts[i] > 1) return false;
}
if (counts[2] > 0) {
if (counts[4] > 0) return false;
if (counts[6] > 0) return false;
if (counts[8] > 0) return false;
if (counts[10] > 0) return false;
}
if (counts[3] > 0) {
if (counts[6] > 0) return false;
if (counts[9] > 0) return false;
}
if (counts[4] > 0) {
if (counts[6] > 0) return false;
if (counts[8] > 0) return false;
if (counts[10] > 0) return false;
}
if (counts[5] > 0) {
if (counts[10] > 0) return false;
}
if (counts[6] > 0) {
if (counts[8] > 0) return false;
if (counts[9] > 0) return false;
if (counts[10] > 0) return false;
}
if (counts[8] > 0) {
if (counts[10] > 0) return false;
}
return true;
}
int maxLength(vector<int>& nums) {
int left = 0;
vector<int> counts(11, 0);
int n = nums.size();
if (n <= 2) return n;
int res = 2;
for (int right = 0; right < n; ++right) {
counts[nums[right]]++;
while (!isValid(counts)) {
counts[nums[left++]]--;
}
res = max(res, right - left + 1);
}
return res;
}
};