@@ -24,3 +24,102 @@ var kSum = function(nums, k) {
24
24
}
25
25
return pq . front ( ) [ 0 ] ;
26
26
} ;
27
+
28
+ // another
29
+
30
+ /**
31
+ * @param {number[] } nums
32
+ * @param {number } k
33
+ * @return {number }
34
+ */
35
+ const kSum = function ( nums , k ) {
36
+ let sum = 0 ,
37
+ n = nums . length ,
38
+ pq = new PriorityQueue ( ( x , y ) => y [ 0 ] < x [ 0 ] )
39
+ for ( let i = 0 ; i < n ; i ++ ) {
40
+ if ( nums [ i ] < 0 ) {
41
+ nums [ i ] *= - 1
42
+ } else {
43
+ sum += nums [ i ]
44
+ }
45
+ }
46
+ if ( k == 1 ) return sum
47
+ nums . sort ( ( x , y ) => x - y )
48
+ pq . push ( [ sum - nums [ 0 ] , 0 ] )
49
+ for ( let i = 2 ; i < k ; i ++ ) {
50
+ let [ x , idx ] = pq . pop ( )
51
+ if ( idx + 1 < n ) {
52
+ pq . push ( [ x + nums [ idx ] - nums [ idx + 1 ] , idx + 1 ] )
53
+ pq . push ( [ x - nums [ idx + 1 ] , idx + 1 ] )
54
+ }
55
+ }
56
+ return pq . peek ( ) [ 0 ]
57
+ }
58
+
59
+ class PriorityQueue {
60
+ constructor ( comparator = ( a , b ) => a > b ) {
61
+ this . heap = [ ]
62
+ this . top = 0
63
+ this . comparator = comparator
64
+ }
65
+ size ( ) {
66
+ return this . heap . length
67
+ }
68
+ isEmpty ( ) {
69
+ return this . size ( ) === 0
70
+ }
71
+ peek ( ) {
72
+ return this . heap [ this . top ]
73
+ }
74
+ push ( ...values ) {
75
+ values . forEach ( ( value ) => {
76
+ this . heap . push ( value )
77
+ this . siftUp ( )
78
+ } )
79
+ return this . size ( )
80
+ }
81
+ pop ( ) {
82
+ const poppedValue = this . peek ( )
83
+ const bottom = this . size ( ) - 1
84
+ if ( bottom > this . top ) {
85
+ this . swap ( this . top , bottom )
86
+ }
87
+ this . heap . pop ( )
88
+ this . siftDown ( )
89
+ return poppedValue
90
+ }
91
+ replace ( value ) {
92
+ const replacedValue = this . peek ( )
93
+ this . heap [ this . top ] = value
94
+ this . siftDown ( )
95
+ return replacedValue
96
+ }
97
+
98
+ parent = ( i ) => ( ( i + 1 ) >>> 1 ) - 1
99
+ left = ( i ) => ( i << 1 ) + 1
100
+ right = ( i ) => ( i + 1 ) << 1
101
+ greater = ( i , j ) => this . comparator ( this . heap [ i ] , this . heap [ j ] )
102
+ swap = ( i , j ) => ( [ this . heap [ i ] , this . heap [ j ] ] = [ this . heap [ j ] , this . heap [ i ] ] )
103
+ siftUp = ( ) => {
104
+ let node = this . size ( ) - 1
105
+ while ( node > this . top && this . greater ( node , this . parent ( node ) ) ) {
106
+ this . swap ( node , this . parent ( node ) )
107
+ node = this . parent ( node )
108
+ }
109
+ }
110
+ siftDown = ( ) => {
111
+ let node = this . top
112
+ while (
113
+ ( this . left ( node ) < this . size ( ) && this . greater ( this . left ( node ) , node ) ) ||
114
+ ( this . right ( node ) < this . size ( ) && this . greater ( this . right ( node ) , node ) )
115
+ ) {
116
+ let maxChild =
117
+ this . right ( node ) < this . size ( ) &&
118
+ this . greater ( this . right ( node ) , this . left ( node ) )
119
+ ? this . right ( node )
120
+ : this . left ( node )
121
+ this . swap ( node , maxChild )
122
+ node = maxChild
123
+ }
124
+ }
125
+ }
0 commit comments