-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2444.cpp
More file actions
58 lines (55 loc) · 1.83 KB
/
2444.cpp
File metadata and controls
58 lines (55 loc) · 1.83 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
class Solution {
public:
long long counting(long long n) {
return n * (n + 1) / 2;
}
long long processing(vector<int>& nums, int start, int end, int minK, int maxK) {
long long res = counting(end - start + 1);
// no minK
int right = start;
while (right <= end) {
if (nums[right] != minK) {
int left = right;
while (right + 1 <= end && nums[right + 1] != minK) right++;
res -= counting(right - left + 1);
}
right++;
}
// no maxK
right = start;
while (right <= end) {
if (nums[right] != maxK) {
int left = right;
while (right + 1 <= end && nums[right + 1] != maxK) right++;
res -= counting(right - left + 1);
}
right++;
}
// no minK and no maxK
right = start;
while (right <= end) {
if (nums[right] != minK && nums[right] != maxK) {
int left = right;
while (right + 1 <= end && nums[right + 1] != minK && nums[right + 1] != maxK) right++;
res += counting(right - left + 1);
}
right++;
}
return res;
}
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
int n = nums.size();
int index = 0;
long long res = 0;
while (index < n) {
if (nums[index] <= maxK && nums[index] >= minK) {
int start = index;
while (index + 1 < n && nums[index + 1] <= maxK && nums[index + 1] >= minK) index++;
// [start, index] is valid
res += processing(nums, start, index, minK, maxK);
}
index++;
}
return res;
}
};