Total Cost to Hire K Workers Problem
Description
LeetCode Problem 2462.
You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the i^th 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
ksessions and hire exactly one worker in each session. - In each hiring session, choose the worker with the lowest cost from either the first
candidatesworkers or the lastcandidatesworkers. 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 the4^thworker because they have the lowest cost[3,2,7,7,**1**,2]. - In the second hiring session, we will choose
1^stworker because they have the same lowest cost as4^thworker 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^51 <= costs[i] <= 10^51 <= k, candidates <= costs.length
Solution
Approach: Min Heap
If 2 * candidates >= n, the two groups cover everybody, so the answer is the sum of the k smallest costs.
Otherwise put the first candidates workers and the last candidates workers in a min heap ordered by (cost, index), and keep two pointers: l = candidates is the next worker on the left, r = n - candidates - 1 the next on the right.
Repeat k times: pop the cheapest worker and add the cost to the answer. If l <= r, refill the heap from the side the popped worker came from (an index below l means the left side): push worker l and advance l, or push worker r and move r down.
Complexity: O(n log n) time and O(n) space, where n is the length of costs.
from heapq import heapify, heappop, heappush
class Solution:
def totalCost(self, costs: List[int], k: int, candidates: int) -> int:
n = len(costs)
if candidates * 2 >= n:
return sum(sorted(costs)[:k])
pq = []
for i, c in enumerate(costs[:candidates]):
heappush(pq, (c, i))
for i in range(n - candidates, n):
heappush(pq, (costs[i], i))
heapify(pq)
l, r = candidates, n - candidates - 1
ans = 0
for _ in range(k):
c, i = heappop(pq)
ans += c
if l > r:
continue
if i < l:
heappush(pq, (costs[l], l))
l += 1
else:
heappush(pq, (costs[r], r))
r -= 1
return ans