Opening the reading…
Opening the reading…
HEAP (PRIORITY QUEUE) › IMPLEMENTARY QUESTIONS
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →At every session, only two parts of the remaining array matter: the first candidates workers and the last candidates workers. The cheapest worker outside those parts cannot be chosen yet, so keeping every remaining worker would add work without adding information. The useful state is therefore two pools, one for each end, each ordered by cost and then by index.
Use a min-heap for each pool. When you hire from the left heap, expose the next unconsidered worker from the left; when you hire from the right heap, expose the next unconsidered worker from the right. Two boundaries mark which workers have already entered a heap. They must never cross, because a worker in the middle must not be inserted into both heaps and counted twice.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n log c) | Each worker is inserted into at most one heap and each selected worker is removed once. There are at most n insertions and k removals, with k <= n, and every heap operation costs O(log c) because a heap never holds more than c workers. |
| Space | O(c) extra, worst case O(n) | The two heaps together hold at most 2c workers, which is O(c); when c is as large as n, this becomes O(n). The returned total is a scalar, so there is no output storage to exclude beyond that. |
#include <climits>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
class Solution {
public:
long long totalCost(vector<int>& costs, int k, int candidates) {
int n = static_cast<int>(costs.size());
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> left;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> right;
int i = 0;
int j = n - 1;
long long answer = 0;
for (int hired = 0; hired < k; ++hired) {
while (left.size() < static_cast<size_t>(candidates) && i <= j) {
left.push({costs[i], i});
++i;
}
while (right.size() < static_cast<size_t>(candidates) && i <= j) {
right.push({costs[j], j});
--j;
}
pair<int, int> leftTop = left.empty()
? make_pair(INT_MAX, -1)
: left.top();
pair<int, int> rightTop = right.empty()
? make_pair(INT_MAX, -1)
: right.top();
if (leftTop.first <= rightTop.first) {
answer += leftTop.first;
left.pop();
} else {
answer += rightTop.first;
right.pop();
}
}
return answer;
}
};The pair is the key detail: priority_queue compares the cost first and the index second, so each heap already chooses the correct worker among equal costs on its own side. The explicit <= comparison between heap tops handles a tie across the two sides by choosing the left top; every index in the left pool is smaller than every index in the right pool while the pools are disjoint.