703. Kth Largest Element in a Stream
题目来源:
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的更多相关文章
- 【Leetcode_easy】703. Kth Largest Element in a Stream
problem 703. Kth Largest Element in a Stream 题意: solution1: priority_queue这个类型没有看明白... class KthLarg ...
- 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+ ...
- [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 ...
- 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 ...
- 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- try,except用法
lst = ["皇阿玛", "皇额娘", "容嬷嬷", "紫薇"] # 模拟for循环 it = lst.__iter_ ...
- Tomcat启动慢原因之一 At least one JAR was scanned for TLDs yet contained no TLDs
Tomcat启动时提示: 信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging f ...
- 【HTML&CSS】搜狐页面代码编写
<!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"& ...
- mysql_real_escape_string与mysqli_real_escape_string
参考 mysql_real_escape_string mysqli_real_escape_string mysql_real_escape_string是用来转义字符的,主要是转义POST或GE ...
- 转:Windows下PHP7安装Redis和Redis扩展phpredis
原文地址:Windows下PHP7安装Redis和Redis扩展phpredis Windows下PHP7安装Redis和Redis扩展phpredis 2016-06-08 17:53:00 标签: ...
- Android 通过URL获取网络资源
1.先在AndroidManifest.xml中注册加入访问因特网服务的权限: <uses-permission android:name="android.permission.IN ...
- oracle常见的等待事件说明
转自 http://blog.itpub.net/29371470/viewspace-1063994/ 1. Buffer busy waits 从本质上讲,这个等待事件的产生仅说明了一个会话在等待 ...
- Django 模板继承extend 标签include block
# block 站网页位置# includ 导入网页标签# extends 导入网页模板 # common_js.html <script src="/static/plugins/j ...
- USTCCourseCommunity 项目介绍
我们的项目名为USTCCourseCommunity,科大课程社区,主要提供课表管理.课程资源管理.课程信息管理.智能排课.轻松评课等方面的服务,旨在为科大师生提供便捷. 科大现有课程服务形式存在的问 ...
- LeetCode题解之Add two numbers
1.题目描述 2.题目描述 题目思路可以参考合并单链表的思路,定义一个全局 进位标志,如果两个数值相加得到需要进位,则将进位标志置为1 . 3.代码 ListNode* addTwoNumbers(L ...