问题描述:

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

思路:

考虑堆的应用,heapq

代码:

 class KthLargest:

     def __init__(self, k: int, nums: List[int]):
nums.sort()
if len(nums) > k - 1:
nums = nums[-k:]
self.k = k
self.nums = nums
heapq.heapify(self.nums) def add(self, val: int) -> int:
if len(self.nums) == self.k - 1:
heapq.heappush(self.nums,val)
elif val > self.nums[0]:
heapq.heapreplace(self.nums,val)
return self.nums[0] # Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)

heapify(x) #以线性时间将一个列表转化为堆 
heappush(heap,item) #往堆中插入一条新的值
item = heappop(heap) #从堆中弹出最小值
item = heap[0] #查看堆中最小值,不弹出
item = heapreplace(heap,item) #弹出并返回最小值,然后将heapqreplace方法中item的值插入到堆中,堆的整体结构不会发生改变

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

  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——要熟悉heapq使用

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

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

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

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

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

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

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

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

  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. Crypko 基于滚动条进行的动画是如何实现的?

    Crypko 网站里面的下拉滚动条进行的动画感觉非常炫,于是研究了一下她的实现,发现她主要是使用了 ScrollMagic 这个库实现了基于滚动条的动画. 为什么这么确定就是用了 ScrollMagi ...

  2. EDM营销必知:电子邮件打开和点击的几组数据

    在EDM营销中,了解一下电子邮件何时被打开和点击很重要.这有助于我们在合适的时间发送邮件出去,从而带来最大化的效果. 1.邮件打开的最高峰在早上8点至9点之间,其次是下午三点到四点.因此,在这个时间发 ...

  3. springmvc+ehcache简单例子

    这里采用的是spring3.2.ehcache2.7.tomcat7.0(必须) 1.web.xml <?xml version="1.0" encoding="U ...

  4. c# WPF——创建带有图标的TreeView

    1.使用数据模板对TreeViewItem进行更改 2.xaml中重写TreeviewItem的控件模板 3.继承TreeViewItem(TreeView中的元素),后台进行控件重写.(介绍此方法) ...

  5. Spring001--事务的传播机制

    Spring事务的传播机制 本文来自于博客:https://blog.csdn.net/yuanlaishini2010/article/details/45792069 一.事务的嵌套概念 事务的嵌 ...

  6. 解决django项目无法连接远程mysql的问题

    我们都知道django项目可以通过修改settings.py文件中的DATABASES这个对象,使用不同的数据库. 如图所示,我们想连接远程的mysql,修改settings.py的配置 然后我们在终 ...

  7. document.domain location.hostname location.host

    document.domain    location.hostname     location.host   :https://www.cnblogs.com/shd-study/p/103031 ...

  8. Java学习day7面向对象编程1-对象和类

    一.Java编程中对象和类的概念 1,什么是类? 答:类是客观存在的,抽象的,概念的东西. 2,什么是对象? 答:对象是具体的,实际的,代表一个事物.例如:车是一个类,汽车,自行车就是他的对象. 关于 ...

  9. 浅谈格雷码(Grey Code)在信息学竞赛中的应用

    1.格雷码的概念 1.性质 格雷码(Grey Code),又叫循环二进制码或反射二进制码,是一种编码方式,它的基本特点是任意两个相邻的格雷码只有一位二进制数不同. 常用的二进制数与格雷码间的转换关系如 ...

  10. host文件无操作权限

    把先用其它启动方式启动电脑,如winpe(网上有制作方法,很简单的),启动后找到FWPKCLNT.SYS所在文件夹, 一般在c/windows/system32/drivers,将drivers文件夹 ...