- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.000 because we will pay 70 to the first worker and 35 to the 3rd worker.

To solve this, we will follow these steps −

qr := a new list

for each pair (q, w) from quality and wage, do

insert (w/q, q) at the end of qr

sort the list qr

cand := a new list, csum := 0

for i in range 0 to K - 1, do

insert -qr[i, 1] into heap cand

csum := csum + qr[i, 1]

ans := csum * qr[K - 1, 0]

for idx in range K to size of quality, do

insert -qr[i, 1] into heap cand

csum := csum + qr[idx, 1] + top element from heap cand and delete it from heap

ans = minimum of ans and (csum * qr[idx][0])

return ans

## Example

Let us see the following implementation to get better understanding

import heapq def solve(quality, wage, K): qr = [] for q, w in zip(quality, wage): qr.append([w/q, q]) qr.sort() cand, csum = [], 0 for i in range(K): heapq.heappush(cand, -qr[i][1]) csum += qr[i][1] ans = csum * qr[K - 1][0] for idx in range(K, len(quality)): heapq.heappush(cand, -qr[idx][1]) csum += qr[idx][1] + heapq.heappop(cand) ans = min(ans, csum * qr[idx][0]) return ans quality = [10,22,5] wage = [70,52,30] K = 2 print(solve(quality, wage, K))

## Input

[10,22,5], [70,52,30], 2

## Output

105

- Related Questions & Answers
- Minimum Cost to Hire K Workers in C++
- Program to find minimum cost to paint fences with k different colors in Python
- Program to find minimum cost to merge stones in Python
- Program to find minimum cost to reach final index with at most k steps in python
- Program to find minimum cost for painting houses in Python
- Program to find minimum cost to connect all points in Python
- Program to find minimum cost to cut a stick in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum deletion cost to avoid repeating letters in Python
- Program to find minimum total cost for equalizing list elements in Python
- Program to find minimum cost to reduce a list into one integer in Python
- Program to find minimum amplitude after deleting K elements in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find minimum cost to pick up gold in given two locations in Python
- Program to find minimum cost to connect each Cartesian coordinates in C++