题目来源:

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. 由Leetcode详解算法 之 动态规划(DP)

    因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...

  2. 正能量:You Are the Best

    Success comes from knowing that you did your best to become the best that you are capable of becomin ...

  3. maven学习知识点汇总

    1. 2.maven自动建立目录骨架 首先进入目录结构:  PS C:\WINDOWS\system32> cd C:\Users\10563\Desktop\test 然后输入自动构建命令:  ...

  4. CSS 几款比较常用的翻转特效

    第一个:360度翻转特效 <style>* { margin:0; padding:0; } .aa { width: 220px; height: 220px; margin: 0 au ...

  5. ORACLE数据泵还原(IMPDP命令)

    Oracle数据库还原IMPDP命令是相对于EXPDP命令的,方向是反向的.即对于数据库备份进行还原操作.一.知晓IMPDP命令 C:\>impdp -help Import: Release ...

  6. 如何利用fiddler篡改发送请求和截取服务器信息

    一.断点的两种方式 1.before response:在request请求未到达服务器之前打断 2.after response:在服务器响应之后打断 二.全局打断 1.全局打断就是中断fiddle ...

  7. SQLServer SELECT @@IDENTITY 遇到的坑

    经常在写存储过程的时候获取当前插入后的ID都会用  @@IDENTITY 但是今天在用 @@IDENTITY的时候涉及到当前数据的插入会有insert触发器发生时,发现与实际插入的ID值对不上,网上查 ...

  8. Asp.net单点登录解决方案

    原文出处:http://www.cnblogs.com/wu-jian 主站:Passport集中验证服务器,DEMO中为:http://www.passport.com/ 分站:http://www ...

  9. 新建一个去除storyboard的项目

    新建一个去除storyboard的项目 1. 新建项目并删除 *.storyboard 以及与之相关的杂项 2. 设置 UIWindow 的 rootViewController 复制粘贴代码如下 s ...

  10. Man's Best Friend: The Science Behind the Dog and Human Relationship

    http://info.thinkfun.com/stem-education/mans-best-friend-the-science-behind-the-dog-and-human-relati ...