Skip to content

Commit

Permalink
finish 0703
Browse files Browse the repository at this point in the history
  • Loading branch information
benben6515 committed May 30, 2022
1 parent d506bc7 commit 4ec90d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ since 2021.10.10 / Benben
- 0509.Fibonacci Number
- 0543.Diameter of Binary Tree
- 0572.Subtree of Another Tree
- 0703.Kth Largest Element in a Stream
- 0704.Binary Search
- 0905.Sort Array By Parity
- 1694.Reformat Phone Number
Expand Down
40 changes: 40 additions & 0 deletions leetcode-easy/0703-kth-largest-element-in-a-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @param {number} k
* @param {number[]} nums
*/
/**
* @param {number} val
* @return {number}
*/
class KthLargest {
constructor(k, nums) {
this.k = k;
this.pq = new MinPriorityQueue();

for(let num of nums) {
this.add(num)
}
}
add(val) {
if (this.pq.size() < this.k){
this.pq.enqueue(val);
return this.pq.front().element
}
let min = this.pq.front().element;
if (val > min){
this.pq.dequeue();
this.pq.enqueue(val);
}
return this.pq.front().element;
}
}

/**
* Your KthLargest object will be instantiated and called as such:
* var obj = new KthLargest(k, nums)
* var param_1 = obj.add(val)
*/

// 2022/05/30 done.
// Runtime: 231 ms, faster than 61.13% of JavaScript online submissions for Kth Largest Element in a Stream.
// Memory Usage: 51.7 MB, less than 54.10% of JavaScript online submissions for Kth Largest Element in a Stream.

0 comments on commit 4ec90d0

Please sign in to comment.