Total Cost to Hire K Workers - Problem

You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the i-th worker.

You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules:

  • You will run k sessions and hire exactly one worker in each session.
  • In each hiring session, choose the worker with the lowest cost from either the first candidates workers or the last candidates workers. Break the tie by the smallest index.
  • If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.
  • A worker can only be chosen once.

Return the total cost to hire exactly k workers.

Input & Output

Example 1 — Basic Case
$ Input: costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
Output: 11
💡 Note: Session 1: First 4 = [17,12,10,2], Last 4 = [2,11,20,8]. Pick min = 2 (index 3). Session 2: First 4 = [17,12,10,7], Last 4 = [2,11,20,8]. Pick min = 2 (index 5). Session 3: First 4 = [17,12,10,7], Last 3 = [11,20,8]. Pick min = 7. Total = 2+2+7 = 11
Example 2 — Small Array
$ Input: costs = [1,2,4,1], k = 3, candidates = 3
Output: 4
💡 Note: Since candidates=3 covers most of array, we pick 3 cheapest: 1, 1, 2. Total = 1+1+2 = 4
Example 3 — Equal Costs
$ Input: costs = [2,2,2,2,2], k = 2, candidates = 2
Output: 4
💡 Note: All costs are equal, so we pick by smallest index. Choose indices 0 and 1, both cost 2. Total = 2+2 = 4

Constraints

  • 1 ≤ k, candidates ≤ costs.length ≤ 105
  • 1 ≤ costs[i] ≤ 105

Visualization

Tap to expand
Total Cost to Hire K Workers INPUT costs array (indices 0-8): 17 i=0 12 i=1 10 i=2 2 i=3 7 i=4 2 i=5 11 i=6 20 i=7 8 i=8 Candidate regions (c=4): First 4: 17,12,10,2 Last 4: 2,11,20,8 Parameters k = 3 candidates = 4 ALGORITHM STEPS 1 Build Two Min-Heaps Left heap: first 4 workers Right heap: last 4 workers 2 Session 1: Pick min Left min: 2 (i=3) Right min: 2 (i=5) Pick i=3, cost=2 3 Session 2: Pick min Left adds i=4 (7) Left min: 7, Right min: 2 Pick i=5, cost=2 4 Session 3: Pick min Right shrinks, adds i=4 Left min: 7, Right min: 7 Pick i=4, cost=7 Min-Heap Structure: Left: [2,10,12,17] Right: [2,8,11,20] FINAL RESULT Workers Hired: Session 1: Worker i=3, Cost=2 Smallest index tie-break Session 2: Worker i=5, Cost=2 From right candidates Session 3: Worker i=4, Cost=7 Smallest index tie-break Total Cost Calculation: 2 + 2 + 7 = 11 Output: 11 Key Insight: Use two min-heaps to efficiently track minimum costs from both ends of the candidate pool. Left heap tracks first 'candidates' workers, right heap tracks last 'candidates' workers. Time Complexity: O(k log candidates) - Each hire operation involves heap operations on at most 2*candidates elements. TutorialsPoint - Total Cost to Hire K Workers | Optimal Solution (Two Min-Heaps)
Asked in
Amazon 15 Google 12 Microsoft 8
23.5K Views
Medium Frequency
~25 min Avg. Time
834 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen