Date and Time: Nov 11, 2024, 13:11 (EST)
Link: https://leetcode.com/problems/total-cost-to-hire-k-workers/
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.
-
-
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.
-
1 <= costs.length <= 10^5
-
1 <= costs[i] <= 10^5
-
1 <= k, candidates <= costs.length
-
We create two minHeap
left[], right[]
to keep track of the left, right part ofcosts
, each minHeap has lengthcandidates
. -
We compare the lowest element (top) for these two minHeaps, then
pop()
the smallest element from that minHeap and add new element into that minHeap. -
Repeat
k
times above.
Notice that we compare l <= r
when we are adding elements into minHeap instead of l < r
. Look at Example 1, after we add 2
into rHeap = [2, 11, 20, 8]
, r = 4
, if we don't allow l = r
, after we pop the first element 2
from lHeap
, we will not be able to add 7
into lHeap
, because l = 4
as well.
class Solution:
def totalCost(self, costs: List[int], k: int, candidates: int) -> int:
# Maintain two minHeap for left and right by using two pointers i, j
# Each of them should maintain len(candidates) minHeap
# Compare the top of two minHeaps and remove the smallest one, and update that minHeap with new element
# l: [1, 2, 4], r: [2, 4, 1], i = 2, j = 1
# cost = 1, remove 1 from l
# l: [2, 4, 1], r: [2, 4, 1], i = 3, j = 1
# cost = 2, remove 1 from l
# l: [2, 4], r: [2, 4]
# cost = 4, remove 2 from l
# TC: O(n), n = len(costs), SC: O(candidates)
l_minHeap, r_minHeap = [], []
l, r = 0, len(costs)-1
res = 0
while k > 0:
# Add elements into l_minHeap
while len(l_minHeap) < candidates and l <= r:
heapq.heappush(l_minHeap, costs[l])
l += 1
while len(r_minHeap) < candidates and l <= r:
heapq.heappush(r_minHeap, costs[r])
r -= 1
# Compare, add and remove the lower costs
left = l_minHeap[0] if l_minHeap else float("inf")
right = r_minHeap[0] if r_minHeap else float("inf")
if left <= right:
res += left
heapq.heappop(l_minHeap)
else:
res += right
heapq.heappop(r_minHeap)
k -= 1
return res
Time Complexity: n
is the length of costs
.
Space Complexity: candidates
.