Solution: Minimum Cost to Hire K Workers

Let’s solve the Minimum Cost to Hire K Workers problem using the Top K Elements pattern.

Statement

You are given nn workers, each characterized by two attributes:

  • quality[i]: Represents the work quality of the ithi^{th} worker.

  • wage[i]: Represents the minimum wage expectation of the ithi^{th} worker.

You want to hire exactly k workers to form a paid group, and you must follow these payment rules:

  1. Wage expectation: Every worker in the group must be paid at least their minimum wage expectation.

  2. Proportional pay: The pay for each worker must be directly proportional to their quality. For example, if one worker’s quality is twice that of another, they must be paid twice as much.

Your goal is to determine the least amount of money required to hire exactly k workers while satisfying the above conditions.

Constraints

  • n ==== quality.length ==== wage.length

  • 11\leq k \leq n 103\leq 10^3

  • 11\leq quality[i], wage[i] 103\leq 10^3

Solution

This solution uses the top k elements pattern, commonly used when selecting the top k items based on a certain criterion. Here, we need to form a group of exactly k workers and minimize the total cost while ensuring fair payments based on the workers’ wage expectations and quality. Below are some key observations that will help us to solve the problem:

  1. Proportionality condition: All workers in the group must be paid proportionally to their quality. If one worker’s quality is twice that of another, they must be paid twice as much.

  2. Wage-to-quality ratio: For each worker, we calculate a ratio that indicates how much we need to pay per unit of quality. This ratio is defined as: ratio[i]=quality[i]/wage[i]ratio[i]=quality[i]/wage[i]​

  3. Greedy approach: To minimize the total wage, we process workers based on their wage-to-quality ratios in ascending order. Once we select k workers, we fix the highest wage-to-quality ratio among them. All workers are paid according to this ratio, ensuring proportionality.

  4. Optimization using a heap: We can use a max heap to efficiently manage the total quality of selected workers. This allows us to keep track of the workers with the smallest total quality, which is important for minimizing the total wage. The max heap allows us to quickly access the largest quality (at the top of the heap) and remove it in constant time, ensuring that only k workers remain.

The steps of the algorithm are given below:

  1. Creates a list of tuple workers for each worker. Sort these tuples based on the ratios in ascending order to process workers with the lowest ratio first. Each tuple contains:

    1. The worker’s wage-to-quality ratio (w/q)

    2. The worker’s quality (q)

  2. Create a max heap heap to store the qualities of the selected workers.

  3. Initialize the below variables:

    1. A variable total_quality is used to track the total quality of the workers in the heap

    2. A variable min_cost to store the minimum cost encountered during the process

  4. Iterate through the sorted list of workers based on their ratios. For each worker:

      1. Add their quality (as a negative value to simulate max heap behavior) to the heap.

      2. Update the total_quality by adding the current worker’s quality.

  5. After adding a worker, check if the heap contains more than k workers. If so, remove the worker with the highest quality (i.e., the smallest negative value) to minimize the total quality used in cost calculations.

  6. Once the heap contains exactly k workers, compute the cost of hiring them. This is done by multiplying the highest wage-to-quality ratio (of the current worker) by the total_quality of the selected group. Update the min_cost if the current one is lower than the previously recorded minimum.

  7. After processing all workers, return the min_cost calculated.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.