File tree
Expand file treeCollapse file tree1 file changed
+28
-0
lines changed Expand file treeCollapse file tree1 file changed
+28
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 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 | +}; |
You can’t perform that action at this time.
0 commit comments