Skip to content

Commit ba163d3

Browse files
committed
Time: 5 ms (84.53%), Space: 65.9 MB (40.94%) - LeetHub
1 parent c58a230 commit ba163d3

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long countSubarrays(int[] nums, int k) {
3+
int maxi = Integer.MIN_VALUE;
4+
for (int num : nums) maxi = Math.max(maxi, num);
5+
int left = 0;
6+
long maxiOccurence = 0;
7+
long res = 0;
8+
for (int right = 0; right < nums.length; right++) {
9+
if (nums[right] == maxi) maxiOccurence++;
10+
while (maxiOccurence >= k) {
11+
if (nums[left] == maxi) maxiOccurence--;
12+
left++;
13+
}
14+
res += left;
15+
}
16+
return res;
17+
}
18+
}

0 commit comments

Comments
 (0)