问题描述:

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. python-判断一个字符串是不是小数

    ''' 1.判断小数点个数是不是1 count 2.分割,判断小数点右边是不是整数 3.判断小数点左边 1.是整数is_digit 2.如果是负整数,按照'-'符号右边的是不会整数 ''' def i ...

  2. Linux_SquidProxyServer代理服务器

    目录 目录 Squid proxy server Web proxy server operating principle Squid features Setup squid server Setu ...

  3. Java各类型占字节数

    byte 1字节short 2字节int 4字节long 8字节float 4字节double 8字节char 2字节boolean 1字节 其中,换算关系: 1GB=1024MB 1MB=1024K ...

  4. object数据类型

    1 object数据类型是dataframe中特殊的数据类型,当某一列出现数字.字符串.特殊字符和时间格式两种及以上时,就会出现object类型,即便把不同类型的拆分开,仍然是object类型. 如下 ...

  5. Binder进程与线程ProcessState以及IPCThreadState

    ProcessState以及IPCThreadState ProcessState是负责打开Binder节点并做mmap映射,IPCThreadState是负责与Binder驱动进行具体的命令交互. ...

  6. spring包

    下载的spring包中文件及各种包众多,在项目中往往只有部分是我们必须的,如果不清楚什么时候需要什么包的话,看看下面就知道了. aspectj目录 下是在Spring框架下使用aspectj的源代码和 ...

  7. c++中关于类的长度的猜想

    在无意中,我偶然发现了类的长度并不是由函数的类型及个数决定,也并非是2的倍数.4的倍数. 在翻阅资料中,我得出了一些我认为可能的猜想. 我们先来看一串代码 #include<iostream&g ...

  8. JDK11 | 第一篇 : JDK11 介绍

    文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/cOqRVlDgOqfDfKtkk1JGxw 一.简介 北京时间 2018年9 月 26 日 ...

  9. npm搭建vue全过程

    如何在Window下安装node\cnpm,并安装vue.js,创建项目 参考链接:https://blog.csdn.net/Corey_mengxiaodong/article/details/8 ...

  10. java 标识接口的作用

    标识接口的作用 标识接口是没有任何方法和属性的接口.标识接口不对实现它的类有任何语义上的要求,它仅仅表明实现它的类属于一个特定的类型. 标接口在Java语言中有一些很著名的应用,例如我们常用的Arra ...