Skip to content
Problem 2462 · LeetCode 2462(opens in a new tab)

Total Cost to Hire K Workers

MediumArrays

Workers stand in a row, and costs[i] is what it costs to hire worker i. You will hire exactly k of them, one per round.

In each round, only the first candidates workers and the last candidates workers still in the row may be picked. Hire the cheapest of them; if two cost the same, hire the one standing further left. The hired worker leaves the row. If fewer than 2 × candidates workers remain, every one of them is a candidate.

Return the total cost of the k hires.

Example 1

Input:  costs = [9, 4, 6, 1, 8, 3, 7, 5], k = 3, candidates = 2
Output: 12
Why:    hire 4 (front), then 5 and 3 (back)
2 more examples and the constraints

Example 2

Input:  costs = [6, 2, 5, 3], k = 3, candidates = 3
Output: 10
Why:    the groups overlap, so all are candidates: 2 + 3 + 5

Example 3

Input:  costs = [3, 8, 8, 3], k = 2, candidates = 1
Output: 6
Why:    the two 3s tie; the left one goes first

Constraints

  • 1 ≤ length of costs ≤ 100,000
  • 1 ≤ costs[i] ≤ 100,000
  • 1 ≤ k, candidates ≤ length of costs

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Simulate each roundO(k · n)
  2. Two min-heapsO((k + c) log c)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 workers and k = 100,000Verdict
Simulate each roundO(k · n)
Correct, but far too slow
Two min-heapsO((k + c) log c)
Best; only edges move

n = number of workers, k = hires, c = candidates.

Edge cases to test

  • Groups overlap. When 2 × candidates ≥ n, each worker must be in only one heap; lo <= hi makes sure of that.
  • Equal costs. [3, 8, 8, 3] with candidates 1: the left 3 is hired first.
  • Hire everyone. k = n gives the sum of all costs, whatever the order.
  • Cheapest in the middle. A cheap worker in the middle waits until the front or back group reaches it.
  • Large totals. The sum can pass 2,147,483,647, so use a 64-bit total.

Common mistakes

  • Pushing the same worker into both heaps when the two groups overlap.
  • Breaking a tie in favour of the right heap, which hires the larger index.
  • Summing into a 32-bit int and overflowing on big inputs.

How to talk through it

  1. 1Clarify. How are ties broken? What happens when the front and back groups overlap? How big can the total get?
  2. 2Give the simulation. Scan both groups each round and remove the hire: O(k · n), too slow at 100,000.
  3. 3Spot what changes. Only one worker joins a group per hire, at the edge of the middle. Two heaps track the cheapest of each group.
  4. 4Code the two heaps. Refill with lo and hi while lo <= hi, compare the tops, let the left win ties.
  5. 5Test out loud. Run the overlapping groups, a tie, and k equal to the number of workers.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?