Skip to content

Commit 0b5af69

Browse files
authored
Update 992-subarrays-with-k-different-integers.js
1 parent 64dd4ef commit 0b5af69

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

992-subarrays-with-k-different-integers.js

+28
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,31 @@ const subarraysWithKDistinct = function(A, K) {
2222
}
2323
return res
2424
}
25+
26+
// another
27+
28+
/**
29+
* @param {number[]} A
30+
* @param {number} K
31+
* @return {number}
32+
*/
33+
const subarraysWithKDistinct = function (A, K) {
34+
const atMostK = (A, K) => {
35+
let left = 0,
36+
right = 0,
37+
counter = 0,
38+
count = [],
39+
result = 0
40+
while (right < A.length) {
41+
let currentR = A[right++]
42+
!count[currentR] ? ((count[currentR] = 1), counter++) : count[currentR]++
43+
while (counter > K) {
44+
let currentL = A[left++]
45+
if (--count[currentL] == 0) counter--
46+
}
47+
result += right - left
48+
}
49+
return result
50+
}
51+
return atMostK(A, K) - atMostK(A, K - 1)
52+
}

0 commit comments

Comments
 (0)