问题描述:

Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Your KthLargest class will have a constructor which accepts an integer kand an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.

Example:

int k = 3;
int[] arr = [4,5,8,2];
KthLargest kthLargest = new KthLargest(3, arr);
kthLargest.add(3);   // returns 4
kthLargest.add(5);   // returns 5
kthLargest.add(10);  // returns 5
kthLargest.add(9);   // returns 8
kthLargest.add(4);   // returns 8

Note: 
You may assume that nums' length ≥ k-1 and k ≥ 1.

思路:

考虑堆的应用,heapq

代码:

 class KthLargest:

     def __init__(self, k: int, nums: List[int]):
nums.sort()
if len(nums) > k - 1:
nums = nums[-k:]
self.k = k
self.nums = nums
heapq.heapify(self.nums) def add(self, val: int) -> int:
if len(self.nums) == self.k - 1:
heapq.heappush(self.nums,val)
elif val > self.nums[0]:
heapq.heapreplace(self.nums,val)
return self.nums[0] # Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)

heapify(x) #以线性时间将一个列表转化为堆 
heappush(heap,item) #往堆中插入一条新的值
item = heappop(heap) #从堆中弹出最小值
item = heap[0] #查看堆中最小值,不弹出
item = heapreplace(heap,item) #弹出并返回最小值,然后将heapqreplace方法中item的值插入到堆中,堆的整体结构不会发生改变

Python3解leetcode Kth Largest Element in a Stream的更多相关文章

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

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

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

  4. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

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

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

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

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

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

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

随机推荐

  1. Week13 - 376. Wiggle Subsequence

    Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...

  2. maven(一) maven到底是什么

    为了方便自己查找,这里转载他人文章,原文出处http://www.cnblogs.com/whgk/p/7112560.html 我记得在搞懂maven之前看了几次重复的maven的教学视频.不知道是 ...

  3. DlgResToDlgTemplate 的代码,提取EXE中的资源,然后转化成C的字符串数组

    代码来源:https://www.codeproject.com/Articles/13330/Using-Dialog-Templates-to-create-an-InputBox-in-C #i ...

  4. kali安装教程

    首先在vm里面新建虚拟机,直接选择典型,然后下一步.   1   2 然后到了这一步,选择中间的安装程序光盘镜像文件,然后去文件里面找你自己下载的镜像,这时候可能系统会出现无法检测此光盘镜像中的操作系 ...

  5. nvm-windows编译源码 go遇到的问题

    异常: Microsoft Windows [Version 10.0.17134.1006] (c) Microsoft Corporation. All rights reserved. C:\U ...

  6. 微信小程序 scroll-View 动态定位之scroll-into-view

  7. python中的包和文件夹的区别

    python的模块,就不得不说包(package),package是module的集合,在一个package中有很多的module, 还是以之前的index.py与baiduHq.py模块为案例,说明 ...

  8. python 正确复制list,克隆list 的各种方案

    推荐4种方法 --------------------------------------------------------------- 方法一:extend L = [1, 2, 3] List ...

  9. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 (转)

    阅读前提:本文分析的是源码,所以至少读者要熟悉它们的接口使用,同时,对于并发,读者至少要知道 CAS.ReentrantLock.UNSAFE 操作这几个基本的知识,文中不会对这些知识进行介绍.Jav ...

  10. LeetCode Lect7 堆及其应用

    概述 堆是一颗完全二叉树.分为大根堆(父节点>=所有的子节点)和小根堆(父节点<=所有的子节点). 插入.删除堆顶都是O(logN),查询最值是O(1). 完全二叉树(Complete B ...