-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path63_countsubarrayswithfixedbounds.cpp
More file actions
34 lines (31 loc) · 1.33 KB
/
63_countsubarrayswithfixedbounds.cpp
File metadata and controls
34 lines (31 loc) · 1.33 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
//https://leetcode.com/problems/count-subarrays-with-fixed-bounds/
class Solution {
public:
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
// initialize some variables
int n = nums.size();
int leftBound = -1;
int lastMin = -1, lastMax = -1;
long long count = 0;
for (int i = 0; i < n; i++) {
// check if nums[i] is within the range [minK, maxK]
if (nums[i] >= minK && nums[i] <= maxK) {
// if nums[i] is equal to minK, update lastMin to the current index i
lastMin = (nums[i] == minK) ? i : lastMin;
// if nums[i] is equal to maxK, update lastMax to the current index i
lastMax = (nums[i] == maxK) ? i : lastMax;
// update the count by the number of valid subarrays between leftBound and the
// smaller of lastMin and lastMax
count += max(0, min(lastMin, lastMax) - leftBound);
} else {
// if nums[i] is not within the range [minK, maxK], update leftBound to i
leftBound = i;
// reset lastMin and lastMax to -1
lastMin = -1;
lastMax = -1;
}
}
// return the final count of valid subarrays
return count;
}
};