Values arrive one at a time. After each arrival, report the kth largest value seen so far, counting repeats as separate values.
A starting batch of values may be supplied before the stream begins.
kthLargestStream(3, {4, 5, 8, 2}, {3, 5, 10, 9, 4})
The wrong ones are this same code with between two and five lines changed. Some of those changes do not compile. There is no Run button: running all three would turn this into a vote rather than a reading.
| 1 | vector<int> kthLargestStream(int k, const vector<int>& seed, |
| 2 | const vector<int>& arriving) { |
| 3 | |
| 4 | priority_queue<int, vector<int>, greater<int>> keep; |
| 5 | |
| 6 | for (int x : seed) { |
| 7 | |
| 8 | keep.push(x); |
| 9 | |
| 10 | if ((int)keep.size() >= k) { |
| 11 | keep.pop(); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | vector<int> ans; |
| 16 | |
| 17 | for (int x : arriving) { |
| 18 | |
| 19 | keep.push(x); |
| 20 | |
| 21 | if ((int)keep.size() > k) { |
| 22 | } |
| 23 | |
| 24 | ans.push_back(keep.top()); |
| 25 | } |
| 26 | |
| 27 | return ans; |
| 28 | } |
| 1 | vector<int> kthLargestStream(int k, const vector<int>& seed, |
| 2 | const vector<int>& arriving) { |
| 3 | |
| 4 | priority_queue<int, vector<int>, greater<int>> keep; |
| 5 | |
| 6 | for (int x : seed) { |
| 7 | |
| 8 | keep.push(x); |
| 9 | |
| 10 | if ((int)keep.size() > k) { |
| 11 | keep.pop(); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | vector<int> ans; |
| 16 | |
| 17 | for (int x : arriving) { |
| 18 | |
| 19 | keep.push(x); |
| 20 | |
| 21 | if ((int)keep.size() > k) { |
| 22 | keep.pop(); |
| 23 | } |
| 24 | |
| 25 | ans.push_back(keep.top()); |
| 26 | } |
| 27 | |
| 28 | return ans; |
| 29 | } |
| 1 | vector<int> kthLargestStream(int k, const vector<int>& seed, |
| 2 | const vector<int>& arriving) { |
| 3 | |
| 4 | priority_queue<int, vector<int>, greater<int>> keep; |
| 5 | |
| 6 | for (int x : seed) { |
| 7 | |
| 8 | keep.push(x); |
| 9 | |
| 10 | if (!((int)keep.size() > k)) { |
| 11 | keep.pop(); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | vector<int> ans; |
| 16 | |
| 17 | for (int x : arriving) { |
| 18 | |
| 19 | keep.push(x); |
| 20 | |
| 21 | if ((int)keep.size() > k) { |
| 22 | } |
| 23 | |
| 24 | ans.push_back(keep.top()); |
| 25 | } |
| 26 | |
| 27 | return ans; |
| 28 | } |