Skip to content

Commit c8c5f18

Browse files
authored
Create 2444. Count Subarrays With Fixed Bounds1 (#778)
2 parents a46c066 + 26c0b00 commit c8c5f18

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
4+
long long count = 0;
5+
int lastMinK = -1, lastMaxK = -1, lastInvalid = -1;
6+
7+
for (int i = 0; i < nums.size(); i++) {
8+
if (nums[i] < minK || nums[i] > maxK) {
9+
lastInvalid = i;
10+
}
11+
if (nums[i] == minK) {
12+
lastMinK = i;
13+
}
14+
if (nums[i] == maxK) {
15+
lastMaxK = i;
16+
}
17+
if (lastMinK != -1 && lastMaxK != -1) {
18+
count += max(0, min(lastMinK, lastMaxK) - lastInvalid);
19+
}
20+
}
21+
22+
return count;
23+
}
24+
};

0 commit comments

Comments
 (0)