Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-08-30 #345

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-08-30

gongpeione commented 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 插件

SaraadKun commented 2 years ago

998. 最大二叉树 II

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