File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
minimum-amount-of-time-to-collect-garbage Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/minimum-amount-of-time-to-collect-garbage/
14
+
13
15
https://leetcode.cn/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/
14
16
15
17
https://leetcode.cn/problems/validate-binary-tree-nodes/
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments