Skip to content

Commit fbbe204

Browse files
committed
https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
1 parent f00d7f2 commit fbbe204

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
14+
1315
https://leetcode.cn/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/
1416

1517
https://leetcode.cn/problems/validate-binary-tree-nodes/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { groupBy } from "../deps.ts";
2+
3+
export default function garbageCollection(
4+
garbage: string[],
5+
travel: number[],
6+
): number {
7+
const pre: number[] = Array(travel.length + 1).fill(0);
8+
let mc = 0;
9+
let pc = 0;
10+
let gc = 0;
11+
let me = 0;
12+
let pe = 0;
13+
let ge = 0;
14+
for (const [i, s] of garbage.entries()) {
15+
if (i !== 0) {
16+
pre[i] = pre[i - 1] + travel[i - 1];
17+
}
18+
19+
const counts = groupBy(s);
20+
const nm = counts["M"]?.length ?? 0;
21+
mc += nm;
22+
const np = counts["P"]?.length ?? 0;
23+
pc += np;
24+
const ng = counts["G"]?.length ?? 0;
25+
gc += ng;
26+
27+
if (nm) {
28+
me = i;
29+
}
30+
if (np) {
31+
pe = i;
32+
}
33+
if (ng) {
34+
ge = i;
35+
}
36+
}
37+
return mc + pc + gc + pre[me] + pre[pe] + pre[ge];
38+
}

0 commit comments

Comments
 (0)