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. 宠物属性控制_pet

    classIndex 职业索引 DmgAddPct 根据职业的法伤或攻强来计算宠物增加的物理伤害,增加的伤害值等于玩家法伤或攻强的百分比 SpAddPct 根据职业的法伤或攻强来计算宠物增加的法术伤害 ...

  2. Django中ORM简介与单表数据操作

    一. ORM简介  概念:.ORM框架是用于实现面向对象编程语言种不同类型系统的数据之间的转换 构建模型的步骤:重点 (1).配置目标数据库信息,在seting.py中设置数据库信息 DATABASE ...

  3. testin 测试用例管理平台

    应用信息:应用图标,应用名称,版本号,应用包名,系统平台[Android,ios,Web/H5,小程序,快应用]项目成员:成员帐号[邮箱],成员姓名,成员角色,成员职位,状态[激活,未激活],操作[移 ...

  4. JavaSE 字符串和正则表达式

    根据不懂的自己整理一下,跟着老师进度刷一遍课本,记录琐碎不懂知识 1.StringTokenizer类 String[] mess= {"整数部分","小数部分" ...

  5. 解决在Vue项目中时常因为代码缩进导致页面报错的问题

    前言 如果我们初次使用vue-cli来构建单页SPA应用,在撸代码的过程中有可能会遇到这种因为代码缩进导致 页面报错的问题,导致我们烦不胜烦.接下来我们就来看一看如何解决这个小问题... erro原因 ...

  6. audio进度条

    如上图所示:为效果图 代码如下: <!doctype html><html> <head> <meta name="author" con ...

  7. Day2-异步IO+Scrapy爬虫

    一.异步IO http://www.cnblogs.com/wupeiqi/articles/6229292.html 这篇文章写的不错,展示了多种高并发的方式,从同步执行→多线程→多进程→async ...

  8. 大数据量 与 UI交互时的处理 总结与心得

    [以下均在主线程中操作时]1.UI直接操作,数据量较大时,直接使用UI会非常慢2.数据驱动操作,数据量较大时,数据与UI的交互效率相比“1”提升明显 总结:但以上这两种操作  都会“较长时间”占用主线 ...

  9. Android+Servlet+MySql+JSON实现简单的数据查询操作--C/S架构

    本例简单地实现Android客户端与服务器端交互,主要是通过客户端输入内容(学号)提交到服务器端,服务器端与数据库交互去查询相应信息(姓名).根据这个做个完整的安卓登录是没问题的.本例数据库服务器都采 ...

  10. 教你一招 - 如何安装nopcommerce2.5

    教你一招 - 如何安装nopcommerce2.5 29. 五月 2012 16:22         /          wcf         /          教你一招 . 解决方案    ...