File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} input
3+
* @param {number} minFrequencyForMaxValue
4+
* @return {number}
5+
*/
6+
var countSubarrays = function (input, minFrequencyForMaxValue) {
7+
const maxValue = Math.max(...input);
8+
9+
let left = 0;
10+
let right = 0;
11+
let frequencyMaxValue = 0;
12+
let countSubarrays = 0;
13+
14+
while (right < input.length) {
15+
16+
frequencyMaxValue += (input[right] === maxValue) ? 1 : 0;
17+
18+
while (frequencyMaxValue === minFrequencyForMaxValue) {
19+
frequencyMaxValue -= (input[left] === maxValue) ? 1 : 0;
20+
++left;
21+
}
22+
++right;
23+
24+
countSubarrays += left;
25+
}
26+
27+
return countSubarrays;
28+
};

0 commit comments

Comments
 (0)