File tree 1 file changed +16
-15
lines changed
scripts/algorithms/M/Map Sum Pairs
1 file changed +16
-15
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 121 ms (Top 6.20%) | Memory: 43.8 MB (Top 29.46%)
1
2
var MapSum = function ( ) {
2
- this . hashKeys = new Map ( ) ;
3
- this . hashVals = new Map ( ) ;
3
+ this . hashKeys = new Map ( ) ;
4
+ this . hashVals = new Map ( ) ;
4
5
} ;
5
6
6
7
MapSum . prototype . insert = function ( key , val ) {
7
- let prefix = '' ;
8
- for ( const str of key ) {
9
- prefix += str ;
10
- const hashKey = this . hashKeys . get ( prefix ) ?? new Set ( ) ;
11
- hashKey . add ( key ) ;
12
- ! this . hashKeys . has ( prefix ) && this . hashKeys . set ( prefix , hashKey ) ;
13
- }
14
- this . hashVals . set ( key , val ) ;
8
+ let prefix = '' ;
9
+ for ( const str of key ) {
10
+ prefix += str ;
11
+ const hashKey = this . hashKeys . get ( prefix ) ?? new Set ( ) ;
12
+ hashKey . add ( key ) ;
13
+ ! this . hashKeys . has ( prefix ) && this . hashKeys . set ( prefix , hashKey ) ;
14
+ }
15
+ this . hashVals . set ( key , val ) ;
15
16
} ;
16
17
17
18
MapSum . prototype . sum = function ( prefix ) {
18
- const hashKey = this . hashKeys . get ( prefix ) ;
19
- if ( ! hashKey ) return 0 ;
20
- let sum = 0 ;
21
- hashKey . forEach ( key => sum += this . hashVals . get ( key ) ) ;
22
- return sum ;
19
+ const hashKey = this . hashKeys . get ( prefix ) ;
20
+ if ( ! hashKey ) return 0 ;
21
+ let sum = 0 ;
22
+ hashKey . forEach ( key => sum += this . hashVals . get ( key ) ) ;
23
+ return sum ;
23
24
} ;
You can’t perform that action at this time.
0 commit comments