Skip to content

Commit f508962

Browse files
committed
add ts
1 parent e535e1b commit f508962

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

typescript/0904-fruit-into-baskets.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function totalFruit(fruits: number[]): number {
2+
let count = new Map();
3+
let [left, total, res] = [0, 0, 0];
4+
5+
for (let fruit of fruits) {
6+
count.set(fruit, (count.get(fruit) || 0) + 1);
7+
total++;
8+
9+
while (count.size > 2) {
10+
let f = fruits[left];
11+
count.set(f, count.get(f) - 1);
12+
total--;
13+
left++;
14+
if (!count.get(f)) {
15+
count.delete(f);
16+
}
17+
}
18+
19+
res = Math.max(res, total);
20+
}
21+
22+
return res;
23+
}

0 commit comments

Comments
 (0)