Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum cost to hire k workers in Python
Suppose we have an array called quality for each different worker, and have another array called wages and a value K. The i-th worker has a quality[i] and a minimum wage expectation wage[i]. We want to hire K workers to form a paid group. When we are hiring a group of K workers, we must pay them according to the following rules:
Each worker in the paid group should be paid in the ratio of their quality by comparing with others in the paid group.
Every worker in the paid group must be paid at least their minimum wage expectation.
We have to find the least amount of money needed to form a paid group satisfying the above conditions.
So, if the input is like quality = [10,22,5], wage = [70,52,30] and K = 2, then the output will be 105.0 because we will pay 70 to the first worker and 35 to the 3rd worker.
Algorithm Steps
To solve this, we will follow these steps −
Create a list
qrof wage-to-quality ratios with their corresponding qualitiesSort this list by wage-to-quality ratio in ascending order
Use a max-heap to keep track of the
Kworkers with lowest total qualityFor each possible "captain" (worker with highest ratio in the group), calculate the minimum cost
Return the minimum cost found
Example
Let us see the following implementation to get better understanding ?
import heapq
def solve(quality, wage, K):
# Create list of (wage/quality ratio, quality) pairs
qr = []
for q, w in zip(quality, wage):
qr.append([w/q, q])
qr.sort()
# Initialize heap and sum for first K workers
cand, csum = [], 0
for i in range(K):
heapq.heappush(cand, -qr[i][1]) # Use negative for max heap
csum += qr[i][1]
# Initial answer with first K workers
ans = csum * qr[K - 1][0]
# Try each remaining worker as potential "captain"
for idx in range(K, len(quality)):
heapq.heappush(cand, -qr[idx][1])
csum += qr[idx][1] + heapq.heappop(cand) # Add new, remove largest
ans = min(ans, csum * qr[idx][0])
return ans
# Test the function
quality = [10, 22, 5]
wage = [70, 52, 30]
K = 2
result = solve(quality, wage, K)
print(f"Minimum cost to hire {K} workers: {result}")
Minimum cost to hire 2 workers: 105.0
How It Works
The algorithm works by recognizing that in any valid group of K workers, one worker will have the highest wage-to-quality ratio, acting as the "captain". All workers must be paid at this captain's ratio to satisfy the proportionality constraint.
We sort workers by their wage-to-quality ratio and use a sliding window approach with a max-heap to efficiently find the K workers with the lowest total quality for each possible captain.
Time Complexity
The time complexity is O(N log N) where N is the number of workers, due to sorting and heap operations.
Conclusion
This solution efficiently finds the minimum cost to hire K workers by using a greedy approach with heap data structure. The key insight is that the worker with the highest wage-to-quality ratio in the group determines the payment rate for all workers.
