@@ -2,18 +2,23 @@ import { BinarySearchTree } from "../deps.ts";
2
2
import { traversal_bst_range } from "./traversal_bst_range.ts" ;
3
3
4
4
export default class TweetCounts {
5
- #name_to_bst = new Map < string , BinarySearchTree < number , number > > ( ) ;
6
- constructor ( ) { }
5
+ #name_to_bst = new Map < string , BinarySearchTree < number > > ( ) ;
6
+ #key_to_count = new Map < number , number > ( ) ;
7
7
8
8
recordTweet ( tweetName : string , time : number ) : void {
9
9
const bst = this . #name_to_bst. get ( tweetName ) ??
10
- new BinarySearchTree < number , number > ( ) ;
10
+ new BinarySearchTree < number > ( ) ;
11
11
const node = bst . find ( time ) ;
12
12
if ( node ) {
13
- node . setValue ( node . getValue ( ) + 1 ) ;
13
+ this . #key_to_count. set (
14
+ time ,
15
+ ( this . #key_to_count. get ( time ) ?? 0 ) + 1 ,
16
+ ) ;
17
+
14
18
return ;
15
19
}
16
- bst . insert ( time , 1 ) ;
20
+ bst . insert ( time ) ;
21
+ this . #key_to_count. set ( time , ( this . #key_to_count. get ( time ) ?? 0 ) + 1 ) ;
17
22
this . #name_to_bst. set ( tweetName , bst ) ;
18
23
}
19
24
@@ -38,14 +43,12 @@ export default class TweetCounts {
38
43
const result : number [ ] = Array (
39
44
Math . ceil ( ( endTime - startTime + 1 ) / length ) ,
40
45
) . fill ( 0 ) ;
41
- traversal_bst_range (
42
- bst . root ( ) ,
43
- startTime ,
44
- endTime ,
45
- ( num : number , value : number ) => {
46
- result [ Math . floor ( ( num - startTime + 1 ) / length ) ] += value ;
47
- } ,
48
- ) ;
46
+ traversal_bst_range ( bst . root ( ) , startTime , endTime , ( num : number ) => {
47
+ const time = num ;
48
+ const value = ( this . #key_to_count. get ( time ) ?? 0 ) ;
49
+
50
+ result [ Math . floor ( ( num - startTime + 1 ) / length ) ] += value ;
51
+ } ) ;
49
52
return result ;
50
53
}
51
54
}
0 commit comments