-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountSubarrays.java
More file actions
42 lines (36 loc) · 1.3 KB
/
countSubarrays.java
File metadata and controls
42 lines (36 loc) · 1.3 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
class Solution {
public long countSubarrays(int[] nums, int k) {
int maxElement = 0;
int maxCount = 0;
// Find maxElement and maxCount
for (int num : nums) {
if (num > maxElement) {
maxElement = num;
maxCount = 1;
} else if (num == maxElement) {
maxCount++;
}
}
// If maxCount < k, return 0
if (maxCount < k) {
return 0;
}
long ans = 0;
int count = 0;
// Sliding window approach
for (int left = 0, right = 0; right < nums.length; right++) {
if (nums[right] == maxElement) {
count++; // Increment count if the current element is maxElement
}
// Shrink the window from the left until the count of maxElement in the window is less than k
while (count >= k) {
ans += nums.length - right; // Increment ans by the number of valid subarrays ending at right
if (nums[left] == maxElement) {
count--; // Decrement count if the left element is maxElement
}
left++; // Move the left pointer
}
}
return ans;
}
}