Find the cheapest group of k workers when pay is proportional to quality and meets each minimum wage

Quick Overview

Choose exactly k workers so that every worker earns at least a minimum wage and pay is proportional to quality, then return the lowest total cost. It tests turning the pay rules into a cost formula, picking a good order to process workers, and keeping a running best selection efficiently.

Find the cheapest group of k workers when pay is proportional to quality and meets each minimum wage

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

There are `n` workers. You are given two integer arrays `quality` and `wage` where `quality[i]` is the quality of the `i`th worker and `wage[i]` is the minimum wage expectation for the `i`th worker. We want to hire exactly `k` workers to form a paid group. When hiring a group of `k` workers, we must pay them according to the following rules: - Every worker in the paid group must be paid at least their minimum wage expectation. - In the group, each worker's pay is proportional to their quality compared to other workers in the group. This means the ratio of a worker's pay to their quality is the same for every worker in the group. Given the integer `k`, return the least amount of money needed to form a paid group satisfying the above conditions. ### Function Signature ```python def min_cost_to_hire_workers(quality: list[int], wage: list[int], k: int) -> float: ``` ### Rules - The group has exactly `k` distinct workers. - Pay amounts are real numbers; they do not have to be integers. - Return the minimum total pay as a float. Any answer within `10^-5` of the exact minimum is accepted. ### Constraints - `n == len(quality) == len(wage)` - `1 <= k <= n <= 10^4` - `1 <= quality[i], wage[i] <= 10^4` ### Examples ```text Input: quality = [10,20,5], wage = [70,50,30], k = 2 Output: 105.00000 ``` Hire workers 0 and 2 and pay 70 and 35. Both are paid 7 per unit of quality, and both meet their minimums (70 and 30). The other pairs cost more: workers 0 and 1 need a rate of 7 on a quality of 30 (210), and workers 1 and 2 need a rate of 6 on a quality of 25 (150). ```text Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3 Output: 30.66667 ``` Hire workers 0, 2 and 3. Worker 0 needs at least 4/3 per unit of quality, so the group is paid 4 + 40/3 + 40/3 = 92/3, which is about 30.66667. Any group that includes worker 1 or worker 4 needs a rate of at least 7 and costs 40 or more. ```text Input: quality = [4,2], wage = [8,10], k = 1 Output: 8.00000 ``` A worker hired alone is paid exactly their minimum wage, so the answer is the smallest wage, 8.

Overview: Choose exactly k workers so that every worker earns at least a minimum wage and pay is proportional to quality, then return the lowest total cost. It tests turning the pay rules into a cost formula, picking a good order to process workers, and keeping a running best selection efficiently.

Google logo
Google
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the ith worker.

We want to hire exactly k workers to form a paid group. When hiring a group of k workers, we must pay them according to the following rules:

  • Every worker in the paid group must be paid at least their minimum wage expectation.
  • In the group, each worker's pay is proportional to their quality compared to other workers in the group. This means the ratio of a worker's pay to their quality is the same for every worker in the group.

Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions.

Function Signature

def min_cost_to_hire_workers(quality: list[int], wage: list[int], k: int) -> float:

Rules

  • The group has exactly k distinct workers.
  • Pay amounts are real numbers; they do not have to be integers.
  • Return the minimum total pay as a float. Any answer within 10^-5 of the exact minimum is accepted.

Constraints

  • n == len(quality) == len(wage)
  • 1 <= k <= n <= 10^4
  • 1 <= quality[i], wage[i] <= 10^4

Examples

Input: quality = [10,20,5], wage = [70,50,30], k = 2
Output: 105.00000

Hire workers 0 and 2 and pay 70 and 35. Both are paid 7 per unit of quality, and both meet their minimums (70 and 30). The other pairs cost more: workers 0 and 1 need a rate of 7 on a quality of 30 (210), and workers 1 and 2 need a rate of 6 on a quality of 25 (150).

Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
Output: 30.66667

Hire workers 0, 2 and 3. Worker 0 needs at least 4/3 per unit of quality, so the group is paid 4 + 40/3 + 40/3 = 92/3, which is about 30.66667. Any group that includes worker 1 or worker 4 needs a rate of at least 7 and costs 40 or more.

Input: quality = [4,2], wage = [8,10], k = 1
Output: 8.00000

A worker hired alone is paid exactly their minimum wage, so the answer is the smallest wage, 8.