#857
Minimum Cost to Hire K Workers
HardArrayGreedySortingHeap (Priority Queue)
Approaches
💡
Intuition
Time Space
The brute force approach involves generating all possible combinations of workers and calculating the cost for each combination. This is straightforward but inefficient for larger inputs.
⚙️
Algorithm
3 steps- 1Step 1: Generate all combinations of k workers from the n workers.
- 2Step 2: For each combination, calculate the total cost based on the minimum wage expectations and their qualities.
- 3Step 3: Keep track of the minimum cost found across all combinations.
solution.py13 lines
1# Full working Python code
2from itertools import combinations
3
4def minCostToHireWorkers(quality, wage, k):
5 n = len(quality)
6 min_cost = float('inf')
7 for combo in combinations(range(n), k):
8 total_quality = sum(quality[i] for i in combo)
9 cost = 0
10 for i in combo:
11 cost += max(wage[i] * total_quality / quality[i], wage[i])
12 min_cost = min(min_cost, cost)
13 return min_costSolutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.