作者: 负雪明烛
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. C++栈溢出

    先看一段代码 #include<iostream> using namespace std; #define n 510 void sum(int a,int b) { cout<& ...

  2. 【Go语言学习笔记】包

    包其实是每个大型工程都会使用的模块化工具. 将相关的代码封装成一个包,给其他项目调用,提供不同的功能. GO的设计是将一个文件夹看成一个包,虽然不一定非要用文件夹的名字,但是比较建议. 同一个文件夹下 ...

  3. 一条查询SQL查询语句的执行原理

    先熟悉一下浅而易懂SQL执行的流程图SQL查询过程七步曲 1.查询SQL发送请求 客户端将查询sql按照mysql通信协议传输到服务端.服务端接受到请求后,服务端单起一个线程执行sql 2.判断是否为 ...

  4. IDEA 超实用使用技巧分享

    前言 工欲善其事 ​ 必先利其器 最近受部门的邀请,给入职新人统一培训IDEA,发现有很多新人虽然日常开发使用的是IDEA,但是还是很多好用的技巧没有用到,只是用到一些基本的功能,蛮浪费IDEA这个优 ...

  5. GO 通过进程号输出运行运行信息

    操作系统应用可以使用PID来查找关于进程本身的信息.当进程失败时获取到的PID就非常有价值,这样就可以使用PID跟踪整个系统中的系统日志,如/var/log/messages./var/log/sys ...

  6. Dubbo声明式缓存

    为了进一步提高消费者对用户的响应速度,减轻提供者的压力,Dubbo提供了基于结果的声明式缓存.该缓存是基于消费者端的,所以使用很简单,只需修改消费者配置文件,与提供者无关 一.创建消费者07-cons ...

  7. Kafaka相关命令

    开启zookeeper命令(备注:先进入zookeeper的bin目录) ./zkServer.sh start 关闭zookeeper命令(备注:先进入zookeeper的bin目录) ./zkSe ...

  8. Nginx+ uWSGI +django进行部署

    一:uWSGI的安装 sudo pip install uwsgi 如果安装报错: conda install -c conda-forge uwsgi conda install -c conda- ...

  9. 【C/C++】PAT A1025 Ranking/算法笔记

    题目意思大概是输入一堆人的学号,成绩,给出学号,总排名,考场号,考场内排名. 这是我第一次写的: #include <iostream> #include <algorithm> ...

  10. 深度学习初探——符号式编程、框架、TensorFlow

    一.命令式编程(imperative)和符号式编程(symblic) 命令式: import numpy as np a = np.ones(10) b = np.ones(10) * 2 c = b ...