Skip to content

Commit 527c447

Browse files
committed
✅ [1456] prefix sum
1 parent 6f498ae commit 527c447

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

1456/my_solution.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
* @return {number}
55
*/
66
var maxVowels = function (s, k) {
7-
let l = 0, maxCount = 0, vowelSet = new Set(["a", "e", "i", "o", "u"])
7+
let l = 0, maxCount = 0, vowelSet = new Set(["a", "e", "i", "o", "u"]), acc = []
88
s = s.split("")
99
console.log(s)
1010
console.log(vowelSet)
1111

12-
while ((l + k - 1) < s.length) {
13-
let tmpCount = 0;
14-
for (let i = l; i < (l + k); i++) {
15-
if (vowelSet.has(s[i])) tmpCount++;
16-
}
12+
let tmpSum = 0;
13+
for (let i = 0; i < s.length; i++) {
14+
tmpSum += (vowelSet.has(s[i]) ? 1 : 0)
15+
acc[i] = tmpSum;
16+
}
1717

18-
maxCount = Math.max(maxCount, tmpCount)
18+
console.log(acc)
1919

20+
while ((l + k - 1) < s.length) {
21+
// console.log("___")
22+
// console.log(acc[k - 1])
23+
// console.log((acc[l - 1] ?? 0))
24+
// console.log(acc[k - 1] - (acc[l - 1] ?? 0))
25+
maxCount = Math.max(maxCount, acc[(k + l) - 1] - (acc[l - 1] ?? 0))
2026
l++;
2127
}
2228

@@ -28,8 +34,8 @@ var maxVowels = function (s, k) {
2834
// ['a', 'b', 'c', 'i', 'i', 'i', 'd', 'e', 'f']
2935
let x =
3036
// maxVowels("abciiidef", 3)//3
31-
// maxVowels("aeiou", 2) //2
32-
maxVowels("leetcode", 3) //2
37+
// maxVowels("aeiou", 2) //2
38+
maxVowels("leetcode", 3) //2
3339

34-
console.log("Res")
35-
console.log(x)
40+
console.log("Res")
41+
console.log(x)

1456/solution.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var maxVowels = function (s, k) {
7+
let l = 0, maxCount = 0, vowelSet = new Set(["a", "e", "i", "o", "u"]), acc = []
8+
s = s.split("")
9+
10+
let tmpSum = 0;
11+
for (let i = 0; i < s.length; i++) {
12+
tmpSum += (vowelSet.has(s[i]) ? 1 : 0)
13+
acc[i] = tmpSum;
14+
}
15+
16+
while ((l + k - 1) < s.length) {
17+
maxCount = Math.max(maxCount, acc[(k + l) - 1] - (acc[l - 1] ?? 0))
18+
l++;
19+
}
20+
21+
return maxCount
22+
};

0 commit comments

Comments
 (0)