703. Kth Largest Element in a Stream
Easy

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.

 
"""
1 import heapq
2 def heapsort(iterable):
3 h = []
4 for i in iterable:
5 heapq.heappush(h, i)
6 return [heapq.heappop(h) for i in range(len(h))]
7
8 # method 1: sort to list
9 s = [3, 5, 1, 2, 4, 6, 0, 1]
10 print(heapsort(s))
11 '''
12 [0, 1, 1, 2, 3, 4, 5, 6]
13 '''
"""
import heapq class KthLargest(object): def __init__(self, k, nums):
"""
:type k: int
:type nums: List[int]
"""
self.arr = []
self.k = k
for i in nums:
if len(self.arr) < self.k:
heapq.heappush(self.arr, i)
else:
if i > self.arr[0]:
#if heapq.nsmallest(1, self.arr)[0] < i:
heapq.heapreplace(self.arr, i)
#heapq.heappop(self.arr)
#heapq.heappush(self.arr, i) def add(self, val):
"""
:type val: int
:rtype: int
"""
if len(self.arr) < self.k:
heapq.heappush(self.arr, val)
else:
if val > self.arr[0]:
#if heapq.nsmallest(1, self.arr)[0] < val:
heapq.heapreplace(self.arr, val)
#heapq.heappop(self.arr)
#heapq.heappush(self.arr, val)
assert len(self.arr) == self.k
return self.arr[0]
#return heapq.nsmallest(1, self.arr)[0] # Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)

注意:如果使用heapq.nsmallest(1, self.arr)[0]来返回heap的最小值会有超时问题。

leetcode Kth Largest Element in a Stream——要熟悉heapq使用的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 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+ ...

  5. 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  6. [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 ...

  7. 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 ...

  8. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  9. [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 ...

随机推荐

  1. 33 Python 详解命令解析 - argparse--更加详细--转载

    https://blog.csdn.net/lis_12/article/details/54618868 Python 详解命令行解析 - argparse Python 详解命令行解析 - arg ...

  2. 学习笔记19—dpabi错误集

    1.回归斜边量的时候千万不要用红色标记的地方,而要用紫色标记的地方

  3. JS中innerHTML、outerHTML、innerText 、outerText、value的区别与联系?jQuery中的text()、html()和val()

    一.JS中innerHTML.outerHTML.innerText .outerText.value的区别与联系?jS中设置或者获取所选内容的值:①innerHTML :属性设置或返回该标签内的HT ...

  4. centos 7 安装jdk1.8

    首先下载jdk1.8  去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...

  5. Fiddler 手机抓包介绍

    直接打开tools -> Options 进行设置 点击OK,在这里代理就设置完成,一定要重启软件配置才生效,下面是手机端的设置. 手机端代理设置以三星S4为例子,1.如下图真机三星S4设置:找 ...

  6. echarts画多条一元回归线

    理论上两点一线,只要两个点即可 option = { title: { text: '', left: 'center' }, tooltip: { // trigger: 'item', // fo ...

  7. Spring 拦截器实现+后台原理(MethodInterceptor)

    MethodInterceptor MethodInterceptor是AOP项目中的拦截器(注:不是动态代理拦截器),区别与HandlerInterceptor拦截目标时请求,它拦截的目标是方法. ...

  8. ADO.NET介绍2

    一.Connection对象 Connection对象也称为数据库连接对象,Connection对象的功能是负责对数据源的连接.所有Connection对象的基类都是DbConnection类. Co ...

  9. WinForm下窗体权限设计

    权限设计   笔者不才看了园子里面很多园友写关于权限设计这块内容,那么笔者也在添一笔.这个是笔者在上完软件工程课程后,上交的一篇笔者论文,这里分享给大家交流,当然笔者经验尚浅,若内容有误,请大家指点出 ...

  10. LeetCode--367--有效的完全平方数

    问题描述: 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如  sqrt. 示例 1: 输入:16 输 ...