File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
design-underground-system Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1
1
export default class UndergroundSystem {
2
+ id2info = new Map < number , [ string , number ] > ( ) ;
3
+ station2time = new Map < string , number [ ] > ( ) ;
2
4
constructor ( ) {
3
5
}
4
6
5
7
checkIn ( id : number , stationName : string , t : number ) : void {
8
+ const info : [ string , number ] = [ stationName , t ] ;
9
+ this . id2info . set ( id , info ) ;
6
10
}
7
11
8
12
checkOut ( id : number , stationName : string , t : number ) : void {
13
+ const info = this . id2info . get ( id ) ;
14
+ if ( info ) {
15
+ const key = JSON . stringify ( [ info [ 0 ] , stationName ] ) ;
16
+ const time = this . station2time . get (
17
+ key ,
18
+ ) ?? [ ] ;
19
+ time . push ( t - info [ 1 ] ) ;
20
+ this . station2time . set ( key , time ) ;
21
+ } else {
22
+ throw Error ( "accident" ) ;
23
+ }
9
24
}
10
25
11
26
getAverageTime ( startStation : string , endStation : string ) : number {
27
+ const key = JSON . stringify ( [ startStation , endStation ] ) ;
28
+ const time = this . station2time . get (
29
+ key ,
30
+ ) ;
31
+ if ( ! time ) throw Error ( "accident" ) ;
32
+
33
+ return time . reduce ( ( a , v ) => a + v , 0 ) / time . length ;
12
34
}
13
35
}
You can’t perform that action at this time.
0 commit comments