File tree Expand file tree Collapse file tree 3 files changed +67
-0
lines changed
solution/0700-0799/0703.Kth Largest Element in a Stream Expand file tree Collapse file tree 3 files changed +67
-0
lines changed Original file line number Diff line number Diff line change @@ -349,6 +349,30 @@ class MinHeap {
349
349
*/
350
350
```
351
351
352
+ #### TypeScript
353
+
354
+ ``` ts
355
+ export class KthLargest {
356
+ #pq = new MinPriorityQueue ();
357
+ #k = 0 ;
358
+
359
+ constructor (k : number , nums : number []) {
360
+ this .#k = k ;
361
+ for (const x of nums ) {
362
+ this .#pq .enqueue (x );
363
+ if (this .#pq .size () > k ) this .#pq .dequeue ();
364
+ }
365
+ }
366
+
367
+ add(val : number ): number {
368
+ this .#pq .enqueue (val );
369
+ if (this .#pq .size () > this .#k ) this .#pq .dequeue ();
370
+
371
+ return this .#pq .front ().element ;
372
+ }
373
+ }
374
+ ```
375
+
352
376
<!-- tabs:end -->
353
377
354
378
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -348,6 +348,30 @@ class MinHeap {
348
348
*/
349
349
```
350
350
351
+ #### TypeScript
352
+
353
+ ``` ts
354
+ export class KthLargest {
355
+ #pq = new MinPriorityQueue ();
356
+ #k = 0 ;
357
+
358
+ constructor (k : number , nums : number []) {
359
+ this .#k = k ;
360
+ for (const x of nums ) {
361
+ this .#pq .enqueue (x );
362
+ if (this .#pq .size () > k ) this .#pq .dequeue ();
363
+ }
364
+ }
365
+
366
+ add(val : number ): number {
367
+ this .#pq .enqueue (val );
368
+ if (this .#pq .size () > this .#k ) this .#pq .dequeue ();
369
+
370
+ return this .#pq .front ().element ;
371
+ }
372
+ }
373
+ ```
374
+
351
375
<!-- tabs:end -->
352
376
353
377
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ export class KthLargest {
2
+ #pq = new MinPriorityQueue ( ) ;
3
+ #k = 0 ;
4
+
5
+ constructor ( k : number , nums : number [ ] ) {
6
+ this . #k = k ;
7
+ for ( const x of nums ) {
8
+ this . #pq. enqueue ( x ) ;
9
+ if ( this . #pq. size ( ) > k ) this . #pq. dequeue ( ) ;
10
+ }
11
+ }
12
+
13
+ add ( val : number ) : number {
14
+ this . #pq. enqueue ( val ) ;
15
+ if ( this . #pq. size ( ) > this . #k) this . #pq. dequeue ( ) ;
16
+
17
+ return this . #pq. front ( ) . element ;
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments