Skip to content

Commit 03326f2

Browse files
authored
Update 2962-count-subarrays-where-max-element-appears-at-least-k-times.js
1 parent 0105aaa commit 03326f2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2962-count-subarrays-where-max-element-appears-at-least-k-times.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const countSubarrays = function (nums, k) {
7+
const n = nums.length
8+
const t = Math.max(...nums)
9+
const cnt = {0: -1}
10+
let i = 0, num = 0
11+
for(let j = 0; j < n; j++) {
12+
const e = nums[j]
13+
if(e === t) {
14+
num++
15+
cnt[num] = j
16+
}
17+
}
18+
19+
let res = 0
20+
// console.log(cnt)
21+
for(let i = k; i <= num; i++) {
22+
const preLen = cnt[i - k + 1] - cnt[i - k]
23+
const sufLen = n - cnt[i]
24+
res += preLen * sufLen
25+
}
26+
27+
28+
return res
29+
}
30+
31+
// another
32+
133
/**
234
* @param {number[]} nums
335
* @param {number} k

0 commit comments

Comments
 (0)