Skip to content

Commit 5a4edbc

Browse files
committed
added leetcode daily challenge task #2444 from 3/4/2023
1 parent 168cdd0 commit 5a4edbc

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/task_2444/Solution.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package task_2444;
2+
3+
class Solution {
4+
5+
public long countSubarrays(int[] nums, int minK, int maxK) {
6+
int n = nums.length;
7+
int leftBound = -1;
8+
int lastMin = -1, lastMax = -1;
9+
long count = 0;
10+
for (int i = 0; i < n; i++) {
11+
if (nums[i] >= minK && nums[i] <= maxK) {
12+
lastMin = (nums[i] == minK) ? i : lastMin;
13+
lastMax = (nums[i] == maxK) ? i : lastMax;
14+
count += Math.max(0, Math.min(lastMin, lastMax) - leftBound);
15+
} else {
16+
leftBound = i;
17+
lastMin = -1;
18+
lastMax = -1;
19+
}
20+
}
21+
return count;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)