题目来源:

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. python学习之老男孩python全栈第九期_数据库day001知识点总结 —— MySQL操作数据库以及数据表、基本数据类型、基本增删改查、外键定义以及创建

    一. 学习SQL语句规则以及外键 1. 操作文件夹 create database db2; 创建文件夹 create database db2 default charset utf8; 创建文件夹 ...

  2. ASP.NET MVC 简单事务添加

    ASP.NET MVC 简单事务 //实例化查询上下文 using ( BookStoreEntities db = new BookStoreEntities()) { //找到需要价格和名称的数据 ...

  3. 记录在window平台安装python的第三库(py,whl)

    在下载python的第三库文件的时候,有些库文件有exe的发行版,但是有些第三库并没有找到针对于window的可执行文件安装包即exe文件,而只有源代码文件即py文件,和whl文件. 下面记录一下在w ...

  4. overload与override的区别

    override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访问权限. 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常). 4.存在于父类和子类之 ...

  5. Pwn with File结构体(二)

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 最新版的 libc 中会对 vtable 检查,所以之前的攻击方式 ...

  6. vue使用百度地图

    1.在百度地图申请密钥:http://lbsyun.baidu.com/ 将 <script type="text/javascript" src="http:// ...

  7. Linux 启动脚本及chkconfig命令之自启动服务

    有时我们会碰到这样的情况,系统启动的时候报一大堆无法连接mysql的错误,问题在mysql数据库还没有启动的时候已经启动了一些需要连接mysql数据库的服务.这样我们就得修改启动顺序,把需要连接mys ...

  8. 解决 There are no resources that can be added or removed from the server

    网上下载了一个项目,在eclipse中部署时,加载项目到tomcat中项目名称无法显示,报出There are no resources that can be added or removed fr ...

  9. 初始Flask

    一.Flask介绍(轻量级的框架,非常快速的就能把程序搭建起来) Flask 主要特点小而轻,原生组件几乎为0, 三方提供的组件请参考Django 非常全面,属于短小精悍型框架 Flask是一个基于P ...

  10. 辉光UIView的category

    辉光UIView的category 本人视频教程系类   iOS中CALayer的使用 效果如下: 源码: UIView+GlowView.h 与 UIView+GlowView.m // // UI ...