Skip to content

Commit a35a0e0

Browse files
authored
Update 2024-maximize-the-confusion-of-an-exam.js
1 parent 69fb002 commit a35a0e0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2024-maximize-the-confusion-of-an-exam.js

+25
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,28 @@ const maxConsecutiveAnswers = function(answerKey, k) {
2323
return Math.max(helper(answerKey, 0), helper(answerKey, 1))
2424
};
2525

26+
// another
27+
28+
/**
29+
* @param {string} answerKey
30+
* @param {number} k
31+
* @return {number}
32+
*/
33+
const maxConsecutiveAnswers = function(answerKey, k) {
34+
let s = answerKey
35+
const freq = Array(26).fill(0), n = s.length, A = 'A'.charCodeAt(0)
36+
let res = 0, l = 0, r = 0, maxFreq = 0
37+
while(r < n) {
38+
maxFreq = Math.max(maxFreq, ++freq[s.charCodeAt(r) - A])
39+
if(r - l + 1 - maxFreq > k) {
40+
freq[s.charCodeAt(l) - A]--
41+
l++
42+
}
43+
res = Math.max(res, r - l + 1)
44+
r++
45+
}
46+
47+
return res
48+
};
49+
50+

0 commit comments

Comments
 (0)