Minimum Cost to Hire K Workers

HardGreedy · Sorting · Heap🧩 GreedyLeetCode #857 ↗Updated

Problem

Worker i has a skill level quality[i] and a minimum acceptable pay wage[i]. Hire exactly k workers so that (1) everyone in the group is paid in proportion to their quality and (2) nobody gets less than their minimum. Return the lowest total pay for such a group (answers within 10-5 are accepted).
Inputquality = [10,20,5], wage = [70,50,30], k = 2Output105.0 (pay 70 and 35)
Inputquality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3Output30.66667

1 ≤ k ≤ n ≤ 104  ·  1 ≤ quality[i], wage[i] ≤ 104

Try it yourself

Java
class Solution {
    public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
Proportional pay means one shared rate (pay per unit of quality). The rate must cover the largest wage/quality ratio in the group, so cost = maxRatio × sumQuality. Sort by ratio, let each worker be the “captain” who sets the rate, and keep the k smallest qualities seen so far in a max-heap.
PatternGreedy

Instinct An optimisation where one obvious local rule (earliest end, farthest reach, biggest first) never needs undoing - commit to it, then try hard to break it.

Memory hack Grab the best bite now and never look back - but only once a counterexample hunt fails to make you regret it.

Open the Greedy guide →

Approaches

Brute ForceTry Every Group of k

Time O(C(n,k) · k)Space O(k)

For a fixed group the cheapest valid rate is its largest wage[i]/quality[i], so the group costs maxRatio × sum(quality). Enumerate every k-subset with recursion, tracking the running sum and max ratio, and keep the minimum.

Java
private double best;

public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
    best = Double.MAX_VALUE;
    choose(quality, wage, k, 0, 0, 0, 0.0);
    return best;
}

private void choose(int[] q, int[] w, int k, int start, int picked, long sumQ, double maxR) {
    if (picked == k) {
        best = Math.min(best, sumQ * maxR);
        return;
    }
    for (int i = start; i <= q.length - (k - picked); i++) {
        double ratio = (double) w[i] / q[i];
        choose(q, w, k, i + 1, picked + 1, sumQ + q[i], Math.max(maxR, ratio));
    }
}

Establishes the cost formula, but combinatorial — hopeless beyond tiny n.

At a glance

LevelApproachTimeSpace
Brute ForceTry Every Group of kO(C(n,k) · k)O(k)
BetterFix the Captain, Take k Smallest QualitiesO(n2 log n)O(n)
OptimalSort by Ratio + Max-Heap of QualitiesO(n log n)O(n)
Common pitfalls
  • Sorting by wage or by quality alone — the group cost depends on the ratio.
  • Integer division in wage / quality — cast to double first.
  • Using a min-heap: you must evict the largest quality to keep the sum small.
Interview tips
  • Derive cost = maxRatio × sumQuality first; everything else follows from it.
  • Frame it as “fix one parameter (the captain), optimise the rest greedily”.
  • Mention the cross-multiplication comparator to avoid floating-point sort issues.
Pattern
Sort by one key + heap on another → cost = (max of A) × (sum of B) over a size-k group.
My notes saved in this browser