@@ -111,3 +111,105 @@ const findMaximizedCapital = function(k, W, Profits, Capital) {
111
111
}
112
112
return W ;
113
113
} ;
114
+
115
+ // another
116
+
117
+ /**
118
+ * @param {number } k
119
+ * @param {number } w
120
+ * @param {number[] } profits
121
+ * @param {number[] } capital
122
+ * @return {number }
123
+ */
124
+ const findMaximizedCapital = function ( k , w , profits , capital ) {
125
+ const capPQ = new PQ ( ( a , b ) => a . cap < b . cap )
126
+ const proPQ = new PQ ( ( a , b ) => a . pro > b . pro )
127
+ const n = profits . length
128
+
129
+ for ( let i = 0 ; i < n ; i ++ ) {
130
+ capPQ . push ( { cap : capital [ i ] , pro : profits [ i ] } )
131
+ }
132
+
133
+ while ( k ) {
134
+
135
+ while ( ! capPQ . isEmpty ( ) && capPQ . peek ( ) . cap <= w ) {
136
+ proPQ . push ( capPQ . pop ( ) )
137
+ }
138
+
139
+ if ( proPQ . isEmpty ( ) ) break
140
+
141
+ w += proPQ . pop ( ) . pro
142
+ k --
143
+ }
144
+
145
+
146
+ return w
147
+ } ;
148
+
149
+ class PQ {
150
+ constructor ( comparator = ( a , b ) => a > b ) {
151
+ this . heap = [ ]
152
+ this . top = 0
153
+ this . comparator = comparator
154
+ }
155
+ size ( ) {
156
+ return this . heap . length
157
+ }
158
+ isEmpty ( ) {
159
+ return this . size ( ) === 0
160
+ }
161
+ peek ( ) {
162
+ return this . heap [ this . top ]
163
+ }
164
+ push ( ...values ) {
165
+ values . forEach ( ( value ) => {
166
+ this . heap . push ( value )
167
+ this . siftUp ( )
168
+ } )
169
+ return this . size ( )
170
+ }
171
+ pop ( ) {
172
+ const poppedValue = this . peek ( )
173
+ const bottom = this . size ( ) - 1
174
+ if ( bottom > this . top ) {
175
+ this . swap ( this . top , bottom )
176
+ }
177
+ this . heap . pop ( )
178
+ this . siftDown ( )
179
+ return poppedValue
180
+ }
181
+ replace ( value ) {
182
+ const replacedValue = this . peek ( )
183
+ this . heap [ this . top ] = value
184
+ this . siftDown ( )
185
+ return replacedValue
186
+ }
187
+
188
+ parent = ( i ) => ( ( i + 1 ) >>> 1 ) - 1
189
+ left = ( i ) => ( i << 1 ) + 1
190
+ right = ( i ) => ( i + 1 ) << 1
191
+ greater = ( i , j ) => this . comparator ( this . heap [ i ] , this . heap [ j ] )
192
+ swap = ( i , j ) => ( [ this . heap [ i ] , this . heap [ j ] ] = [ this . heap [ j ] , this . heap [ i ] ] )
193
+ siftUp = ( ) => {
194
+ let node = this . size ( ) - 1
195
+ while ( node > this . top && this . greater ( node , this . parent ( node ) ) ) {
196
+ this . swap ( node , this . parent ( node ) )
197
+ node = this . parent ( node )
198
+ }
199
+ }
200
+ siftDown = ( ) => {
201
+ let node = this . top
202
+ while (
203
+ ( this . left ( node ) < this . size ( ) && this . greater ( this . left ( node ) , node ) ) ||
204
+ ( this . right ( node ) < this . size ( ) && this . greater ( this . right ( node ) , node ) )
205
+ ) {
206
+ let maxChild =
207
+ this . right ( node ) < this . size ( ) &&
208
+ this . greater ( this . right ( node ) , this . left ( node ) )
209
+ ? this . right ( node )
210
+ : this . left ( node )
211
+ this . swap ( node , maxChild )
212
+ node = maxChild
213
+ }
214
+ }
215
+ }
0 commit comments