作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/kth-largest-element-in-a-stream/description/

题目描述

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.

题目大意

实现一个类,这个类能找出一个数据流中第K大的数。

解题方法

小根堆

曾经在亚马逊遇到过的面试题,可惜当时不会,甚至连用堆来实现都没想到。现在知道用堆来实现了。

Python的堆是小根堆,不需要对其进行转换,我们想一想,如果一个堆的大小是k的话,那么最小的数字就在其最前面(即为第k大的数字),只要维护当新来的数字和最前面的这个数字比较即可。

所以我们的工作就是维护一个小根堆,这个小根堆保存的是从第K大的数字到最大的数字。堆的大小即为K。

实现过程比较简单,只要熟悉Python中堆的操作即可。

代码如下:

class KthLargest(object):

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

日期

2018 年 7 月 13 日 —— 早起困一上午,中午必须好好休息才行啊
2018 年 11 月 21 日 —— 又是一个美好的开始

【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)的更多相关文章

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

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

  3. 【Leetcode_easy】703. Kth Largest Element in a Stream

    problem 703. Kth Largest Element in a Stream 题意: solution1: priority_queue这个类型没有看明白... class KthLarg ...

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

  5. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

  6. 703. Kth Largest Element in a Stream

    题目来源: https://leetcode.com/problems/kth-largest-element-in-a-stream/ 自我感觉难度/真实难度: 题意: 这个题目的意思解读了半天,没 ...

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

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

  9. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

随机推荐

  1. [转载]ORA-02287: 此处不允许序号

    原文地址:ORA-02287: 此处不允许序号作者:nowhill 转载自 http://blog.sina.com.cn/s/blog_6d496bad01011dyv.html 开发人员反映序列不 ...

  2. 从for循环到机器码

    def p(*x): print(x) p(type(range), dir(range)) r = range(2); i = iter(r) try: p(next(i)); p(next(i)) ...

  3. html块 布局

    可通过<div>和<span>将html元素组合起来. Html块元素 大多数html元素被定义为块级元素或内联元素. 块级元素在浏览器显示时,通常会以新行来开始(和结束).例 ...

  4. 锁对象-条件对象-synchronized关键字

    1 import java.util.concurrent.locks.Condition; 2 import java.util.concurrent.locks.Lock; 3 import ja ...

  5. 【编程思想】【设计模式】【结构模式Structural】3-tier

    Pyhon版 https://github.com/faif/python-patterns/blob/master/structural/3-tier.py #!/usr/bin/env pytho ...

  6. 【Service】【Database】【MySQL】基础概念

    1. 数据模型:层次模型.网状模型.关系模型 关系模型: 二维关系: 表:row, column 索引:index 视图:view 2. SQL接口:Structured Query Language ...

  7. SpringMvc分析

    1.用户单击某个请求路径,发起一个request请求,此请求会被前端控制器(DispatcherServlet)处理 2.前端控制器(DispatcherServlet)请求处理器映射器(Handle ...

  8. C# 枚举的flags 标志位应用

    枚举有个特性叫标志位,使用方法如下 [Flags] enum Foo { a =1, b = 2, c = 4, d = 8 } 每个值需要为2的n次方,保证多个值的组合不会重复. 这样在判断其中一个 ...

  9. ThreadLocal的使用方法

    ThreadLocal的使用方法 (2011-10-10 22:05:48) 转载▼     概述 ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个 ...

  10. CF812A Sagheer and Crossroads 题解

    Content 有一个十字路口,从最下面的部分开始,逆时针依次标号为 \(1,2,3,4\).每个部分有四个灯,分别为左转的灯.直行的灯.右转的灯以及人行通道灯(只有可能为红灯和绿灯).如果某个部分的 ...