@@ -16,38 +16,39 @@ class MKAverage {
16
16
}
17
17
18
18
addElement ( num : number ) {
19
- if ( this . queue . length === this . m ) {
20
- const willBeDelete : number = this . queue [ 0 ] ;
21
- this . queue . shift ( ) ;
22
- console . log ( "deleteElement" , willBeDelete ) ;
23
- this . total -= willBeDelete ;
24
- this . tree . remove ( willBeDelete ) ;
25
- const root = this . tree . getRoot ( ) ;
26
- if ( ! root ) throw Error ( "null root" ) ;
27
- {
28
- const temp : [ number , number ] [ ] = [ ] ;
29
- for ( const node of InOrderIterator ( root ) ) {
30
- temp . push ( [ node . value , MultiRedBlackTree . getCount ( node ) ] ) ;
31
- }
32
- console . log ( JSON . stringify ( temp ) ) ;
33
- }
34
- }
35
-
36
19
this . queue . push ( num ) ;
37
20
38
21
this . tree . insert ( num ) ;
39
22
this . total += num ;
40
23
this . count ++ ;
41
24
// debugger;
42
- console . log ( "addElement" , num ) ;
25
+ // console.log("addElement", num);
43
26
const root = this . tree . getRoot ( ) ;
44
27
if ( ! root ) throw Error ( "null root" ) ;
45
- {
46
- const temp : [ number , number ] [ ] = [ ] ;
47
- for ( const node of InOrderIterator ( root ) ) {
48
- temp . push ( [ node . value , MultiRedBlackTree . getCount ( node ) ] ) ;
49
- }
50
- console . log ( JSON . stringify ( temp ) ) ;
28
+ // {
29
+ // console.log(root);
30
+ // const temp: [number, number][] = [];
31
+ // for (const node of InOrderIterator(root)) {
32
+ // temp.push([node.value, this.tree.getCount(node.value)]);
33
+ // }
34
+ // console.log(JSON.stringify(temp));
35
+ // }
36
+ if ( this . queue . length === this . m + 1 ) {
37
+ const willBeDelete : number = this . queue [ 0 ] ;
38
+ this . queue . shift ( ) ;
39
+ // console.log("deleteElement", willBeDelete);
40
+ this . total -= willBeDelete ;
41
+ this . tree . remove ( willBeDelete ) ;
42
+ const root = this . tree . getRoot ( ) ;
43
+ if ( ! root ) throw Error ( "null root" ) ;
44
+ // {
45
+ // console.log(root);
46
+ // const temp: [number, number][] = [];
47
+ // for (const node of InOrderIterator(root)) {
48
+ // temp.push([node.value, this.tree.getCount(node.value)]);
49
+ // }
50
+ // console.log(JSON.stringify(temp));
51
+ // }
51
52
}
52
53
}
53
54
@@ -57,29 +58,30 @@ class MKAverage {
57
58
let ret = this . total ;
58
59
const root = this . tree . getRoot ( ) ;
59
60
if ( ! root ) throw Error ( "null root" ) ;
60
- console . log ( "calculateMKAverage" ) ;
61
- {
62
- const temp : [ number , number ] [ ] = [ ] ;
63
- for ( const node of InOrderIterator ( root ) ) {
64
- temp . push ( [ node . value , MultiRedBlackTree . getCount ( node ) ] ) ;
65
- }
66
- console . log ( JSON . stringify ( temp ) ) ;
67
- }
61
+ // console.log("calculateMKAverage");
62
+ // {
63
+ // console.log(root);
64
+ // const temp: [number, number][] = [];
65
+ // for (const node of InOrderIterator(root)) {
66
+ // temp.push([node.value, this.tree.getCount(node.value)]);
67
+ // }
68
+ // console.log(JSON.stringify(temp));
69
+ // }
68
70
69
71
// console.log(root)
70
72
let k = this . k ;
71
73
72
74
for ( const node of InOrderIterator ( root ) ) {
73
75
// console.log(node)
74
- const min = Math . min ( k , MultiRedBlackTree . getCount ( node ) ) ;
76
+ const min = Math . min ( k , this . tree . getCount ( node . value ) ) ;
75
77
ret -= node . value * min ;
76
78
k -= min ;
77
79
if ( k === 0 ) break ;
78
80
}
79
81
k = this . k ;
80
82
for ( const node of reverseInOrderIterator ( root ) ) {
81
83
// debugger;
82
- const min = Math . min ( k , MultiRedBlackTree . getCount ( node ) ) ;
84
+ const min = Math . min ( k , this . tree . getCount ( node . value ) ) ;
83
85
ret -= node . value * min ;
84
86
k -= min ;
85
87
if ( k === 0 ) break ;
0 commit comments