LeetCode 703. Kth Largest Element in a Stream

原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-a-stream/description/

题目:

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 k and 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.

题解:

用minHeap维持size在k.

Time Complexity: KthLargest, O(n * logk). add O(logk). n = nums.length.

Space: O(k).

AC Java:

 1 class KthLargest {
 2     PriorityQueue<Integer> minHeap;
 3     int k;
 4
 5     public KthLargest(int k, int[] nums) {
 6         this.minHeap = new PriorityQueue<Integer>();
 7         this.k = k;
 8         for(int num : nums){
 9             add(num);
10         }
11     }
12
13     public int add(int val) {
14         if(minHeap.size() < k){
15             minHeap.add(val);
16         }else if(minHeap.peek() < val){
17             minHeap.poll();
18             minHeap.add(val);
19         }
20
21         return minHeap.peek();
22     }
23 }
24
25 /**
26  * Your KthLargest object will be instantiated and called as such:
27  * KthLargest obj = new KthLargest(k, nums);
28  * int param_1 = obj.add(val);
29  */

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/9785044.html

时间: 2024-10-29 19:07:53

LeetCode 703. Kth Largest Element in a Stream的相关文章

leetcode 703. Kth Largest Element in a Stream &amp; c++ priority_queue &amp; minHeap/maxHeap

703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap 相关链接 leetcode c++ priority_queue cplusplus c++ priority_queue cnblog 背景知识 ??堆是算法中常用的数据结构之一,其结构是完全二叉树,但实现的方法最常见的是使用数组:这里主要介绍小顶堆,其根元素最小,对于任何一个节点来说,他都比其后代要小:访问器根元素的时间为O(1):树的

703. Kth Largest Element in a Stream

https://leetcode.com/problems/kth-largest-element-in-a-stream/description/ class KthLargest { public: priority_queue<int> q; int k; KthLargest(int k, vector<int> nums) { this->k = k; for (auto &i : nums) add(i); } int add(int val) { if

703. Kth Largest Element in a Stream/215. Kth Largest Element in an Array/

703 非常经典的一个题,假设有一个不断增加的序列,要求输出第K 大的数 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);   // retur

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

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 in the sorted order, not the kth distinct element. Your KthLargest class will have a constructor which accepts an integer k and an integer array nums,

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Example 1:

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

LeetCode 215 : Kth Largest Element in an Array

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. Note: You may assume k is always valid, 1 ≤ k ≤ array's

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example, Given [3,2,1,5,6,4] and k = 2, return 5. 解题思路: 本题是<算法导论>原题,