Open Zheaoli opened 2 years ago
/*
* @lc app=leetcode id=703 lang=javascript
*
* [703] Kth Largest Element in a Stream
*/
// @lc code=start
class KthLargest {
minHeap = null
k = 0
constructor(k, nums) {
this.minHeap = new MinPriorityQueue();
this.k = k;
nums.forEach(n => {
this.minHeap.enqueue(n);
});
}
add(val) {
// put val into min heap
this.minHeap.enqueue(val);
// pop min element if min heap's size is greater than k
while (this.minHeap.size() > this.k) {
this.minHeap.dequeue();
}
return this.minHeap.front().element;
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* var obj = new KthLargest(k, nums)
* var param_1 = obj.add(val)
*/
// @lc code=end
微信id: 弘树 来自 vscode 插件
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
TreeNode pre = new TreeNode(-1), p = pre;
pre.right = root;
while(root != null && root.val > val) {
p = root;
root = root.right;
}
p.right = new TreeNode(val);
p.right.left = root;
return pre.right;
}
}
WeChat: Saraad
2022-08-30