题目来源:

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. Code Signal_练习题_matrixElementsSum

    After they became famous, the CodeBots all decided to move to a new building and live together. The ...

  2. c#如何将子窗体显示到父窗体的容器(panel)控件中

    如何将一个窗体显示到一个容器控件中,刚开始想的比较简单,用窗体容器控件添加一般控件的方法,试了一试,代码如下: Form2 frm = new Form2(); this.panel1.Control ...

  3. vue+vux 父组件控制子组件弹层

    知识点用到了vue父子组件之间的传值,以及使用watch和v-model控制vux中XDialog组件. 需要注意的问题: 1.父组件向子组件传值使用的是props(单向传值),子组件创建props, ...

  4. C#+MapServer相关代码

    //放大的代码: private void MapZoomIn(NameValueCollection queryString) { mapObj map = Session["MapSer ...

  5. 前端 css+js实现返回顶部功能

    描述: 本文主要是讲,通过css+js实现网页中的[返回顶部]功能. 实现代码: HTML: <div> <button onclick="returnTop()" ...

  6. Retrofit+RxJava(2)-基本使用

    首先是抽象的基类 public abstract class BaseApi { public static final String API_SERVER = "服务器地址" p ...

  7. ORACLE学习文档

    转自 http://sparklet.blog.sohu.com/523655.html 数据库被分成一个或多个逻辑部件称作表空间.而表空间又被分成称作段(segment)的逻辑部件.这些段再细分就叫 ...

  8. gitlab 启用HTTPS

    NGINX设置 启用HTTPS 警告 Nginx配置会告诉浏览器和客户端,只需在未来24个月通过安全连接与您的GitLab实例进行通信.通过启用HTTPS,您需要至少在24个月内为您的实例提供安全连接 ...

  9. npm、webpack、Gulp 中文教程

    按顺序阅读 1.npm 模块管理器 2.package.json 文件 3.npm 模块安装机制简介 4.npm scripts 使用指南 5.CommonJS 规范 随着 es6 模块化特性的出现, ...

  10. netty发送和接收数据handler处理器

    netty发送和接收数据handler处理器 主要是继承 SimpleChannelInboundHandler 和 ChannelInboundHandlerAdapter 一般用netty来发送和 ...