题目来源:

https://leetcode.com/problems/kth-largest-element-in-a-stream/

自我感觉难度/真实难度:
题意:

这个题目的意思解读了半天,没搞明白什么意思,后来画了一下图,一下子就明了

分析:
自己的代码:
代码效率/结果:
优秀代码:
class KthLargest:

    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]
代码效率/结果:
自己优化后的代码:
反思改进策略:

1.熟悉了一下怎么使用heapqd的常规函数

2.最后这个

if self.size<self.k:
是不是可以省略呢,size是不是不可能大于K,因为初始化的时候,数组就只有K那么大?后面用的replace,不会变大的
验证了一下,不行的:因为输入的list,有可能是空的,这样会报错,out of index

703. Kth Largest Element in a Stream的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 无法在类...中找到资源".bmp"

    在WinForm中写的一个程序,在项目中添加了一个bmp图片,然后 public void SetSubType(int SubType) { m_subType = SubType; switch ...

  2. 搭建Kafka开发环境

    Kafka版本是:kafka_2.10-0.8.2.1 1.maven工程方式 在pom.xml中配置kafka依赖 1 2 3 4 5 <dependency>     <grou ...

  3. apache ftp server的简单入门(java应用内嵌ftp server)

    Apache Ftp Server:(强调) Apache Ftp Server 是100%纯Java的FTP服务器软件,它采用MINA网络框架开发具有非常好的性能.Apache FtpServer ...

  4. 聊聊 getClientRects 和 getBoundingClientRect 方法

    开始表演 今天来聊一下两个相似的方法,它们就是:getBoundingClientRect().getClientRects(). 只见它们俩手拉手地登上了舞台,一个鞠躬,便开始滔滔不绝起来. 自述 ...

  5. leetcode 之 Same Tree

    1.题目描述 Given two binary trees, write a function to check if they are the same or not. Two binary tre ...

  6. 探讨Oracle分区表

    一年又一年,又到年底了,对于数据库的分区表需要检查一下,有无最大分区,次分区是否需要追加分区,如果程序不是自动追加分区的话,那么年中结算的时候,就会报错. 1.oracle分区主要有五种类型 (1)R ...

  7. python及pandas,numpy等知识点技巧点学习笔记

    python和java,.net,php web平台交互最好使用web通信方式,不要使用Jypython,IronPython,这样的好处是能够保持程序模块化,解耦性好 python允许使用'''.. ...

  8. 使用 Jenkins 和 Team Services 将应用部署到 Linux VM

    持续集成 (CI) 和持续部署 (CD) 是一个管道,可以通过它生成.发布和部署代码. Team Services 针对到 Azure 的部署提供了一组完整的功能完备的 CI/CD 自动化工具. Je ...

  9. 【转】Kettle发送邮件步骤遇到附件名是中文名变成乱码的问题解决办法

    原文:http://www.ukettle.org/thread-607-1-1.html 本帖最后由 大白菜 于 2016-3-7 10:18 编辑 导语:看到群里很多朋友问Kettle发送邮件附件 ...

  10. 优化REST Framework 的 路由 APIView 和ViewSetMixin

    APIview: 我们经常写的是view  这个APIview继承了我们的view,并且对请求进来的信息进行设置, 在APIView这个例子中,调用了drf本身的serializer以及Respons ...