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:
k
sessions and hire exactly one worker in each session.candidates
workers or the last candidates
workers. Break the tie by the smallest index.
costs = [3,2,7,7,1,2]
and candidates = 2
, then in the first hiring session, we will choose the 4th
worker because they have the lowest cost [3,2,7,7,1,2]
.1st
worker because they have the same lowest cost as 4th
worker but they have the smallest index [3,2,7,7,2]
. Please note that the indexing may be changed in the process.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 <= 105
1 <= costs[i] <= 105
1 <= k, candidates <= costs.length
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method finds the absolute lowest cost to hire K workers by checking every single possible combination of workers. We examine each possible group of workers, compute the total cost for that specific group, and ultimately pick the cheapest option we find.
Here's how the algorithm would work step-by-step:
def min_cost_to_hire_k_workers_brute_force(costs, k_workers):
number_of_workers = len(costs)
minimum_total_cost = float('inf')
# Iterate through all possible combinations of workers.
for i in range(1 << number_of_workers):
selected_workers = []
for j in range(number_of_workers):
if (i >> j) & 1:
selected_workers.append(j)
# Only process combinations of size k.
if len(selected_workers) == k_workers:
current_total_cost = 0
# Calculate cost of the current worker group.
for worker_index in selected_workers:
current_total_cost += costs[worker_index]
# Update minimum cost if necessary
minimum_total_cost = min(minimum_total_cost, current_total_cost)
return minimum_total_cost
The trick here is to avoid checking every single combination of workers. We'll use a 'smart' selection process to pick the cheapest workers from both ends of the list and adjust as we go.
Here's how the algorithm would work step-by-step:
import heapq
def total_cost_to_hire_workers(costs, number_of_workers, number_of_candidates):
total_cost = 0
left_group = []
right_group = []
left_index = 0
right_index = len(costs) - 1
# Initial population of the priority queues.
for _ in range(number_of_candidates):
if left_index <= right_index:
heapq.heappush(left_group, costs[left_index])
left_index += 1
else:
break
for _ in range(number_of_candidates):
if left_index <= right_index:
heapq.heappush(right_group, costs[right_index])
right_index -= 1
else:
break
for _ in range(number_of_workers):
# Deciding which group to hire from.
if not right_group or (left_group and left_group[0] <= right_group[0]):
cheapest_worker = heapq.heappop(left_group)
total_cost += cheapest_worker
# Replenish from the left side.
if left_index <= right_index:
heapq.heappush(left_group, costs[left_index])
left_index += 1
else:
# Replenish from the right side.
cheapest_worker = heapq.heappop(right_group)
total_cost += cheapest_worker
if left_index <= right_index:
heapq.heappush(right_group, costs[right_index])
right_index -= 1
return total_cost
Case | How to Handle |
---|---|
Empty costs array | Return 0 immediately as there are no workers to hire. |
k is 0 | Return 0 immediately as no workers need to be hired. |
costs array size is less than candidates | Treat candidates as the size of the costs array; hire workers from the entire array. |
costs array size equals k | Sum all costs in the array and return the result, as all workers must be hired. |
All costs are identical | The algorithm should still function correctly, picking the worker with the smallest index in case of ties. |
Costs contain large integer values that could cause overflow | Use long long to accumulate the total cost to prevent potential integer overflow. |
candidates is larger than the array size divided by 2 | Handle this condition by hiring from the entire list if candidates is too big. |
k is larger than the array size | Return the sum of all costs, as you have to hire everyone. |