File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed
solution/0700-0799/0703.Kth Largest Element in a Stream Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change 1
- export class KthLargest {
1
+ class KthLargest {
2
2
#pq = new MinPriorityQueue ( ) ;
3
3
#k = 0 ;
4
4
5
5
constructor ( k : number , nums : number [ ] ) {
6
6
this . #k = k ;
7
7
for ( const x of nums ) {
8
8
this . #pq. enqueue ( x ) ;
9
- if ( this . #pq. size ( ) > k ) this . #pq. dequeue ( ) ;
9
+ if ( this . #pq. size ( ) > k ) {
10
+ this . #pq. dequeue ( ) ;
11
+ }
10
12
}
11
13
}
12
14
13
15
add ( val : number ) : number {
14
16
this . #pq. enqueue ( val ) ;
15
- if ( this . #pq. size ( ) > this . #k) this . #pq. dequeue ( ) ;
16
-
17
+ if ( this . #pq. size ( ) > this . #k) {
18
+ this . #pq. dequeue ( ) ;
19
+ }
17
20
return this . #pq. front ( ) . element ;
18
21
}
19
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