11class Heap {
22 constructor ( compare ) {
3- this . data = [ ] ;
4- this . compare = compare ;
3+ this . data = [ ] ;
4+ this . compare = compare ;
55 }
6-
6+
77 size ( ) {
8- return this . data . length ;
8+ return this . data . length ;
99 }
10-
10+
1111 peek ( ) {
12- return this . data [ 0 ] ;
12+ return this . data [ 0 ] ;
1313 }
14-
14+
1515 push ( val ) {
16- this . data . push ( val ) ;
17- this . _siftUp ( ) ;
16+ this . data . push ( val ) ;
17+ this . _siftUp ( ) ;
1818 }
19-
19+
2020 pop ( ) {
21- const top = this . peek ( ) ;
22- const bottom = this . data . pop ( ) ;
23- if ( this . data . length > 0 ) {
24- this . data [ 0 ] = bottom ;
25- this . _siftDown ( ) ;
26- }
27- return top ;
21+ const top = this . peek ( ) ;
22+ const bottom = this . data . pop ( ) ;
23+ if ( this . data . length > 0 ) {
24+ this . data [ 0 ] = bottom ;
25+ this . _siftDown ( ) ;
26+ }
27+ return top ;
2828 }
29-
29+
3030 _siftUp ( ) {
31- let i = this . data . length - 1 ;
32- const node = this . data [ i ] ;
33- while ( i > 0 ) {
34- const parent = Math . floor ( ( i - 1 ) / 2 ) ;
35- if ( this . compare ( node , this . data [ parent ] ) ) {
36- this . data [ i ] = this . data [ parent ] ;
37- i = parent ;
38- } else break ;
39- }
40- this . data [ i ] = node ;
31+ let i = this . data . length - 1 ;
32+ const node = this . data [ i ] ;
33+ while ( i > 0 ) {
34+ const parent = Math . floor ( ( i - 1 ) / 2 ) ;
35+ if ( this . compare ( node , this . data [ parent ] ) ) {
36+ this . data [ i ] = this . data [ parent ] ;
37+ i = parent ;
38+ } else break ;
39+ }
40+ this . data [ i ] = node ;
4141 }
42-
42+
4343 _siftDown ( ) {
44- let i = 0 ;
45- const node = this . data [ 0 ] ;
46- const length = this . data . length ;
47-
48- while ( true ) {
49- let left = 2 * i + 1 ;
50- let right = 2 * i + 2 ;
51- let swap = i ;
52-
53- if ( left < length && this . compare ( this . data [ left ] , this . data [ swap ] ) ) {
54- swap = left ;
55- }
56- if ( right < length && this . compare ( this . data [ right ] , this . data [ swap ] ) ) {
57- swap = right ;
44+ let i = 0 ;
45+ const node = this . data [ 0 ] ;
46+ const length = this . data . length ;
47+
48+ while ( true ) {
49+ let left = 2 * i + 1 ;
50+ let right = 2 * i + 2 ;
51+ let swap = i ;
52+
53+ if ( left < length && this . compare ( this . data [ left ] , this . data [ swap ] ) ) {
54+ swap = left ;
55+ }
56+ if ( right < length && this . compare ( this . data [ right ] , this . data [ swap ] ) ) {
57+ swap = right ;
58+ }
59+ if ( swap === i ) break ;
60+
61+ this . data [ i ] = this . data [ swap ] ;
62+ i = swap ;
5863 }
59- if ( swap === i ) break ;
60-
61- this . data [ i ] = this . data [ swap ] ;
62- i = swap ;
63- }
64-
65- this . data [ i ] = node ;
64+
65+ this . data [ i ] = node ;
6666 }
67- }
68-
69- var MedianFinder = function ( ) {
67+ }
68+
69+ var MedianFinder = function ( ) {
7070 this . small = new Heap ( ( a , b ) => a > b ) ; // max heap
7171 this . large = new Heap ( ( a , b ) => a < b ) ; // min heap
72- } ;
73-
74- MedianFinder . prototype . addNum = function ( num ) {
72+ } ;
73+
74+ MedianFinder . prototype . addNum = function ( num ) {
7575 this . small . push ( num ) ;
76-
76+
7777 if (
78- this . small . size ( ) > 0 &&
79- this . large . size ( ) > 0 &&
80- this . small . peek ( ) > this . large . peek ( )
78+ this . small . size ( ) > 0 &&
79+ this . large . size ( ) > 0 &&
80+ this . small . peek ( ) > this . large . peek ( )
8181 ) {
82- this . large . push ( this . small . pop ( ) ) ;
82+ this . large . push ( this . small . pop ( ) ) ;
8383 }
84-
84+
8585 if ( this . small . size ( ) > this . large . size ( ) + 1 ) {
86- this . large . push ( this . small . pop ( ) ) ;
86+ this . large . push ( this . small . pop ( ) ) ;
8787 }
8888 if ( this . large . size ( ) > this . small . size ( ) + 1 ) {
89- this . small . push ( this . large . pop ( ) ) ;
89+ this . small . push ( this . large . pop ( ) ) ;
9090 }
91- } ;
92-
93- MedianFinder . prototype . findMedian = function ( ) {
91+ } ;
92+
93+ MedianFinder . prototype . findMedian = function ( ) {
9494 if ( this . small . size ( ) > this . large . size ( ) ) {
95- return this . small . peek ( ) ;
95+ return this . small . peek ( ) ;
9696 }
9797 if ( this . large . size ( ) > this . small . size ( ) ) {
98- return this . large . peek ( ) ;
98+ return this . large . peek ( ) ;
9999 }
100100 return ( this . small . peek ( ) + this . large . peek ( ) ) / 2 ;
101- } ;
102-
101+ } ;
0 commit comments