2462. Total Cost to Hire K
- Hardness: \(\color{orange}\textsf{Medium}\)
- Ralated Topics:
Array
、Two Pointer
、Heap (Priority Queue)
、Simulation
- \(\color{blue}\textsf{Weekly Contest 318}\)
一、題目
You are given a 0-indexed integer array costs
where costs[i]
is the cost of hiring the ith
worker.
You are also given two integers k
and candidates
. We want to hire exactly k
workers according to the following rules:
- You will run
k
sessions and hire exactly one worker in each session. - In each hiring session, choose the worker with the lowest cost from either the first
candidates
workers or the lastcandidates
workers. Break the tie by the smallest index.- For example, if
costs = [3,2,7,7,1,2]
andcandidates = 2
, then in the first hiring session, we will choose the4th
worker because they have the lowest cost[3,2,7,7,1,2]
. - In the second hiring session, we will choose
1st
worker because they have the same lowest cost as4th
worker but they have the smallest index[3,2,7,7,2]
. Please note that the indexing may be changed in the process.
- For example, if
- If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.
- A worker can only be chosen once.
Return the total cost to hire exactly
k
workers.
Example 1:
- Input: costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
- Output: 11
- Explanation: We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
Example 2:
- Input: costs = [1,2,4,1], k = 3, candidates = 3
- Output: 4
- Explanation: We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [1,2,4,1]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
Constraints:
1 <= costs.length <= 10^5
1 <= costs[i] <= 10^5
1 <= k, candidates <= costs.length
二、分析
- 將數組左右兩邊
candidates
長度的數字加入各別的min heap
中,按順序比較兩個min heap
,將小的加入和中。 - 注意當從
min heap
中取值之後,要移動指針,將新的值放入min heap
,除非當左右指針相撞(代表整個數組都在candidate
的範圍內)。
三、解題
1. Heap (Priority Queue)
- Time complexity: \(O(n)\)
- Space complexity: \(O(n)\)
long long totalCost(vector<int>& costs, int k, int candidates) {
priority_queue<int, vector<int>, greater<int>> front, end; // 宣告兩個 min heap
long long res = 0;
int cnt = 0;
int i = 0, j = costs.size()-1;
front.push(INT_MAX); // 當 min heap 為空時,必定傳回另一個 min heap 的值
end.push(INT_MAX);
while (i <= j && cnt < candidates) {
front.push(costs[i++]); // 左指針移動
if (i <= j) end.push(costs[j--]); // 右指針移動,左右指針相撞,代表已經包含整個數組
cnt++;
}
while (k--) { // 取 k 個數字
if (front.top() <= end.top()) {
res += front.top();
front.pop();
if (i <= j) front.push(costs[i++]); // 左右指針相撞,不再加入新的值
} else {
res += end.top();
end.pop();
if (i <= j) end.push(costs[j--]); // 左右指針相撞,不再加入新的值
}
}
return res;
}