File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
solution/0200-0299/0215.Kth Largest Element in an Array Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -309,4 +309,30 @@ impl Solution {
309
309
310
310
<!-- solution:end -->
311
311
312
+ <!-- solution:start -->
313
+
314
+ ### Solution 3: Max priority heap
315
+
316
+ <!-- tabs:start -->
317
+
318
+ #### TypeScript
319
+
320
+ ``` ts
321
+ function findKthLargest(nums : number [], k : number ): number {
322
+ const maxPQ = new MaxPriorityQueue ();
323
+ for (const x of nums ) {
324
+ maxPQ .enqueue (x );
325
+ }
326
+
327
+ let res = 0 ;
328
+ while (k -- ) res = maxPQ .dequeue ().element ;
329
+
330
+ return res ;
331
+ }
332
+ ```
333
+
334
+ <!-- tabs:end -->
335
+
336
+ <!-- solution:end -->
337
+
312
338
<!-- problem:end -->
Original file line number Diff line number Diff line change @@ -301,4 +301,30 @@ impl Solution {
301
301
302
302
<!-- solution:end -->
303
303
304
+ <!-- solution:start -->
305
+
306
+ ### Solution 3: Max priority heap
307
+
308
+ <!-- tabs:start -->
309
+
310
+ #### TypeScript
311
+
312
+ ``` ts
313
+ function findKthLargest(nums : number [], k : number ): number {
314
+ const maxPQ = new MaxPriorityQueue ();
315
+ for (const x of nums ) {
316
+ maxPQ .enqueue (x );
317
+ }
318
+
319
+ let res = 0 ;
320
+ while (k -- ) res = maxPQ .dequeue ().element ;
321
+
322
+ return res ;
323
+ }
324
+ ```
325
+
326
+ <!-- tabs:end -->
327
+
328
+ <!-- solution:end -->
329
+
304
330
<!-- problem:end -->
Original file line number Diff line number Diff line change
1
+ function findKthLargest ( nums : number [ ] , k : number ) : number {
2
+ const maxPQ = new MaxPriorityQueue ( ) ;
3
+ for ( const x of nums ) {
4
+ maxPQ . enqueue ( x ) ;
5
+ }
6
+
7
+ let res = 0 ;
8
+ while ( k -- ) res = maxPQ . dequeue ( ) . element ;
9
+
10
+ return res ;
11
+ }
You can’t perform that action at this time.
0 commit comments