Skip to main content
Back to problems
#857
Hard Algorithms

Minimum cost to hire k workers

Array Greedy Sorting Heap (Priority Queue)
63.6% acceptance
Feb 22, 2026
3047
406
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. To hire 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 must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker. Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.

Solution

Rust
Time O(n log n)
Space O(n)
LeetCode
solution.rs
/*
 * 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. To hire 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 must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
 * Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 10-5 of the actual answer will be accepted.
 * Example 1:
 * Input: quality = [10,20,5], wage = [70,50,30], k = 2
 * Output: 105.00000
 * Explanation: We pay 70 to 0th worker and 35 to 2nd worker.
 * Example 2:
 * Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
 * Output: 30.66667
 * Explanation: We pay 4 to 0th worker, 13.33333 to 2nd and 3rd workers separately.
 * Constraints:
 * n == quality.length == wage.length
 * 1 <= k <= n <= 104
 * 1 <= quality[i], wage[i] <= 104
 */

use std::collections::BinaryHeap;
impl Solution {
  pub fn mincost_to_hire_workers(quality: Vec<i32>, wage: Vec<i32>, k: i32) -> f64 {
    let k = k as usize;
    let n = quality.len();
    // Sort by wage[i]/quality[i] ratio
    let mut workers: Vec<(f64, i32)> = (0..n)
      .map(|i| (wage[i] as f64 / quality[i] as f64, quality[i]))
      .collect();
    workers.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
    let mut heap = BinaryHeap::new(); // max-heap of quality
    let mut quality_sum = 0i64;
    let mut ans = f64::MAX;
    for (ratio, q) in workers {
      heap.push(q);
      quality_sum += q as i64;
      if heap.len() > k {
        quality_sum -= heap.pop().unwrap() as i64;
      }
      if heap.len() == k {
        ans = ans.min(ratio * quality_sum as f64);
      }
    }
    ans
  }
}