File tree 3 files changed +94
-0
lines changed
solution/0700-0799/0703.Kth Largest Element in a Stream
3 files changed +94
-0
lines changed Original file line number Diff line number Diff line change @@ -349,6 +349,39 @@ class MinHeap {
349
349
*/
350
350
```
351
351
352
+ #### TypeScript
353
+
354
+ ``` ts
355
+ 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 ) {
364
+ this .#pq .dequeue ();
365
+ }
366
+ }
367
+ }
368
+
369
+ add(val : number ): number {
370
+ this .#pq .enqueue (val );
371
+ if (this .#pq .size () > this .#k ) {
372
+ this .#pq .dequeue ();
373
+ }
374
+ return this .#pq .front ().element ;
375
+ }
376
+ }
377
+
378
+ /**
379
+ * Your KthLargest object will be instantiated and called as such:
380
+ * var obj = new KthLargest(k, nums)
381
+ * var param_1 = obj.add(val)
382
+ */
383
+ ```
384
+
352
385
<!-- tabs:end -->
353
386
354
387
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -348,6 +348,39 @@ class MinHeap {
348
348
*/
349
349
```
350
350
351
+ #### TypeScript
352
+
353
+ ``` ts
354
+ 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 ) {
363
+ this .#pq .dequeue ();
364
+ }
365
+ }
366
+ }
367
+
368
+ add(val : number ): number {
369
+ this .#pq .enqueue (val );
370
+ if (this .#pq .size () > this .#k ) {
371
+ this .#pq .dequeue ();
372
+ }
373
+ return this .#pq .front ().element ;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * Your KthLargest object will be instantiated and called as such:
379
+ * var obj = new KthLargest(k, nums)
380
+ * var param_1 = obj.add(val)
381
+ */
382
+ ```
383
+
351
384
<!-- tabs:end -->
352
385
353
386
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ 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 ) {
10
+ this . #pq. dequeue ( ) ;
11
+ }
12
+ }
13
+ }
14
+
15
+ add ( val : number ) : number {
16
+ this . #pq. enqueue ( val ) ;
17
+ if ( this . #pq. size ( ) > this . #k) {
18
+ this . #pq. dequeue ( ) ;
19
+ }
20
+ return this . #pq. front ( ) . element ;
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Your KthLargest object will be instantiated and called as such:
26
+ * var obj = new KthLargest(k, nums)
27
+ * var param_1 = obj.add(val)
28
+ */
You can’t perform that action at this time.
0 commit comments