leetcode Kth Largest Element in a Stream——要熟悉heapq使用
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使用的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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+ ...
- 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- webservice跨域问题
在webconfig里面加上 <httpProtocol> <customHeaders> <add name="Access-Co ...
- _itemmod_exchange_item
物品强化说明 表说明 `item`物品ID `exchangedItem` 升级后的物品ID `upFlag` 物品等级模式0或1,一般为0 `reqId` 需求ID `chance` 升级几率 `c ...
- python (协程)生产者,消费者
#coding=utf- import gevent from gevent.queue import Queue, Empty import time tasks = Queue(maxsize=) ...
- C# txt文件的读取与写入
C#创建记事本方法一://创建对象 FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileM ...
- C.字符串(字符)操作
1.memchr 检测的是一段内存,strchr 检测的是一个字符串 如果一段内存中有0x0的话,显然不能用strchr去查找的.建议看看两个函数的原型 strchr会停在 '\0',memchr不会 ...
- C++.sprintf
ZC:sprintf,sprintf_s 1.经测试 sprintf,是会在字符串的最后 加上'\0'的,∴ 不用担心 字符串的结尾的问题 2. 3. 4. 5.
- PostgreSQL安装及使用教程一(exe安装方式)
下载安装 百度搜索PostgreSQL,进入官网,选择相应版本的图形化安装程序(BigSQL)安装即可 连接数据库 对数据库操作有两种方式,一种是通过命令行工具psql,另一种是通过图形化界面pgAd ...
- PowerDesign的简单使用方法
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- DEBUG(2)--函数的输入参数要做适当的检查
今天在调试程序时发现,在单步运行的情况下,程序执行没有问题,但是直接运行就会出问题.出问题的代码如下 for(int col=0;col<=9;++col) { int killid=Pos ...
- 牛客OI周赛4-提高组 C 战争(war)
战争(war) 思路: 二分答案, 找到第一个不满足条件的位置 首先对于一个值来说, 所有这个值的区间肯定有交区间, 然后在这个交区间内不能出现比它小的数 所以我们check时从大的值开始考虑, 求出 ...