Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);   // returns 4
kthLargest.add(5);   // returns 5
kthLargest.add(10);  // returns 5
kthLargest.add(9);   // returns 8
kthLargest.add(4);   // returns 8

Note: 
You may assume that nums' length ≥ k-1 and k ≥ 1.

Approach #1: C++.[priority_queue]

class KthLargest {
public:
KthLargest(int k, vector<int> nums) {
size = k;
for (int i = 0; i < nums.size(); ++i) {
pq.push(nums[i]);
if (pq.size() > k) pq.pop();
}
} int add(int val) {
pq.push(val);
if (pq.size() > size) pq.pop();
return pq.top();
} private:
priority_queue<int, vector<int>, greater<int>> pq;
int size;
}; /**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

  

Approach #2: Java.[BST]

class KthLargest {
TreeNode root;
int k; public KthLargest(int k, int[] nums) {
this.k = k;
for (int num : nums) root = add(root, num);
} public int add(int val) {
root = add(root, val);
return findKthLargest();
} private TreeNode add(TreeNode root, int val) {
if (root == null) return new TreeNode(val);
root.count++;
if (val < root.val) root.left = add(root.left, val);
else root.right = add(root.right, val);
return root;
} public int findKthLargest() {
int count = k;
TreeNode walker = root; while (count > 0) {
int pos = 1 + (walker.right != null ? walker.right.count : 0);
if (count == pos) break;
if (count > pos) {
count -= pos;
walker = walker.left;
} else if (count < pos)
walker = walker.right;
}
return walker.val;
} class TreeNode {
int val, count = 1;
TreeNode left, right;
TreeNode(int v) { val = v; }
}
} /**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/

  

Approach #3: Python.[priority_queue].

class KthLargest(object):

    def __init__(self, k, nums):
"""
:type k: int
:type nums: List[int]
"""
self.pool = nums
self.k = k
heapq.heapify(self.pool)
while len(self.pool) > k:
heapq.heappop(self.pool) def add(self, val):
"""
:type val: int
:rtype: int
"""
if len(self.pool) < self.k:
heapq.heapq.push(self.pool, val)
elif val > self.pool[0]:
heapq.heapreplace(self.pool, val)
return self.pool[0] # Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)

  

Kth Largest Element in a Stream的更多相关文章

  1. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  2. [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  3. [LeetCode&Python] Problem 703. Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  4. LeetCode - 703. Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  5. leetcode Kth Largest Element in a Stream——要熟悉heapq使用

    703. Kth Largest Element in a Stream Easy Design a class to find the kth largest element in a stream ...

  6. LeetCode - Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  7. 【Leetcode_easy】703. Kth Largest Element in a Stream

    problem 703. Kth Largest Element in a Stream 题意: solution1: priority_queue这个类型没有看明白... class KthLarg ...

  8. Python3解leetcode Kth Largest Element in a Stream

    问题描述: Design a class to find the kth largest element in a stream. Note that it is the kth largest el ...

  9. leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap

    703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap 相关链接 leetcode c+ ...

随机推荐

  1. Why Use C++/CLI?

    来源:http://www.asawicki.info/Download/Productions/Publications/CPP_CLI_tutorial.pdf Why Use C++/CLI? ...

  2. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

  3. 修正IE6中FIXED不能用的办法,转载

    .fixed-top /* 头部固定 */{position:fixed;bottom:auto;top:0px;} .fixed-bottom /* 底部固定 */{position:fixed;b ...

  4. case_for_if 各种嵌套相结合

    注明:本文是参考其他相关文章 整理,完全尊重原著作 #!/bin/bash usage() { cat << EOF EOF } main() { echo "猜分数赢大奖(0- ...

  5. nodejs 版本dockerfile 文件制作,和常用命令

    Dockerfile 如下 官方的node6.3的版本有点难下载,建议去网易蜂巢  https://c.163.com/hub pull hub.c.163.com/library/node:6.9 ...

  6. 【linux】自动删除7天前的文件

    下面的脚本是删除/home目录下7天前的文件 #!/bin/bash -exec rm -f {} \; 把这个脚本保存在/tmp目录下,命名为:clearfile.sh 加入计划任务 crontab ...

  7. Vue:实践学习笔记(6)——使用SLOT分发内容

    Vue:实践学习笔记(6)——使用SLOT分发内容 Slot Slot是什么 Slot是父子组件的通讯方式,可以将父组件的内容显示到子组件之中. 使用SLOT前 比如我在定义组件的时候,在里面输入了X ...

  8. POJ - 3278 Catch That Cow 【BFS】

    题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...

  9. 使用electron静默打印

    1.使用electron打印的理由 很多情况下程序中使用的打印都是用户无感知的.并且想要灵活的控制打印内容,往往需要借助打印机给我们提供的api再进行开发,这种开发方式非常繁琐,并且开发难度较大. e ...

  10. Dubbo之消费者

    在写 dubbbo调用时候 <dubbo:reference  不能有空格! 项目结构: pom: <project xmlns="http://maven.apache.org ...