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.html
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:spa
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.code
這道題讓咱們在數據流中求第K大的元素,跟以前那道Kth Largest Element in an Array很相似,但不一樣的是,那道題的數組是肯定的,不會再增長元素,這樣肯定第K大的數字就比較簡單。而這道題的數組是不斷在變大的,因此每次第K大的數字都在不停的變化。那麼咱們其實只關心前K大個數字就能夠了,因此咱們能夠使用一個最小堆來保存前K個數字,當再加入新數字後,最小堆會自動排序,而後把排序後的最小的那個數字去除,則堆中仍是K個數字,返回的時候只需返回堆頂元素便可,參見代碼以下:htm
解法一:blog
class KthLargest { public: KthLargest(int k, vector<int> nums) { for (int num : nums) { q.push(num); if (q.size() > k) q.pop(); } K = k; } int add(int val) { q.push(val); if (q.size() > K) q.pop(); return q.top(); } private: priority_queue<int, vector<int>, greater<int>> q; int K; };
咱們也能夠使用multiset來作,利用其可重複,且自動排序的功能,這樣也能夠達到最小堆的效果,參見代碼以下:排序
解法二:element
class KthLargest { public: KthLargest(int k, vector<int> nums) { for (int num : nums) { st.insert(num); if (st.size() > k) st.erase(st.begin()); } K = k; } int add(int val) { st.insert(val); if (st.size() > K) st.erase(st.begin()); return *st.begin(); } private: multiset<int> st; int K; };
相似題目:leetcode
Kth Largest Element in an Arrayget
參考資料:
https://leetcode.com/problems/kth-largest-element-in-a-stream