703. Kth Largest Element in a Stream

class KthLargest { public: KthLargest(int k, vector<int> nums) { pos = k; for (int num : nums) pq.push(num); } int add(int val) { pq.push(val); queue<int> q; // store elements pushed out from the priority queue for (int i = 0; i < pos; i++){ q.push(pq.top()); pq.pop(); } int res = pq.top(); pq.pop(); while(!q.empty()){ pq.push(q.front()); q.pop(); } return res; } private: int pos; priority_queue<int> pq; }
Leetcode probably has a bug.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.