Watch 10 video solutions for Total Cost to Hire K Workers, a medium level problem involving Array, Two Pointers, Heap (Priority Queue). This walkthrough by codestorywithMIK has 11,748 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a 0-indexed integer array costs where costs[i] is the cost of hiring the ith worker.
You are also given two integers k and candidates. We want to hire exactly k workers according to the following rules:
k sessions and hire exactly one worker in each session.candidates workers or the last candidates workers. Break the tie by the smallest index.
costs = [3,2,7,7,1,2] and candidates = 2, then in the first hiring session, we will choose the 4th worker because they have the lowest cost [3,2,7,7,1,2].1st worker because they have the same lowest cost as 4th worker but they have the smallest index [3,2,7,7,2]. Please note that the indexing may be changed in the process.Return the total cost to hire exactly k workers.
Example 1:
Input: costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4 Output: 11 Explanation: We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [17,12,10,2,7,2,11,20,8]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2. - In the second hiring round we choose the worker from [17,12,10,7,2,11,20,8]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4. - In the third hiring round we choose the worker from [17,12,10,7,11,20,8]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers. The total hiring cost is 11.
Example 2:
Input: costs = [1,2,4,1], k = 3, candidates = 3 Output: 4 Explanation: We hire 3 workers in total. The total cost is initially 0. - In the first hiring round we choose the worker from [1,2,4,1]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers. - In the second hiring round we choose the worker from [2,4,1]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2. - In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [2,4]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4. The total hiring cost is 4.
Constraints:
1 <= costs.length <= 105 1 <= costs[i] <= 1051 <= k, candidates <= costs.lengthProblem Overview: You are given an array costs where each value represents the cost of hiring a worker. You must hire exactly k workers. At each step, you can only choose from the first candidates workers or the last candidates workers that have not yet been hired. The goal is to minimize the total hiring cost while following this rule.
Approach 1: Min-Heap Simulation (O((k + candidates) log candidates) time, O(candidates) space)
This approach simulates the hiring process using a heap (priority queue). Maintain two candidate pools: one from the left side and one from the right side of the array. Push their costs into a min-heap along with their indices. Each time you hire a worker, pop the smallest cost from the heap and add it to the total. Then expand the same side (left or right) by pushing the next available worker into the heap. The heap always gives the cheapest valid worker while dynamically maintaining the hiring window. This approach works well because the heap guarantees efficient retrieval of the minimum cost worker during each of the k hiring steps.
Approach 2: Two Pointers with Sorted Segments (O(n log n) time, O(n) space)
This method uses two pointers to track the active candidate windows from both ends of the array. First build sorted segments for the first and last candidate pools so the cheapest worker from either side can be selected quickly. After hiring a worker from one side, advance the corresponding pointer and insert the next worker into the sorted structure. The process repeats until k workers are hired. The idea relies on maintaining ordered candidate pools while the pointers gradually move inward through the array. Sorting or maintaining ordered containers makes selecting the smallest cost straightforward.
Recommended for interviews: The min-heap simulation is the approach most interviewers expect. It models the hiring rule directly and demonstrates strong understanding of priority queues and simulation. Explaining the window expansion and how the heap always tracks the cheapest available candidate shows solid algorithmic reasoning. The two-pointer sorted approach also works but is typically less intuitive and involves heavier preprocessing.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Min-Heap Simulation | O((k + candidates) log candidates) | O(candidates) | General case. Best balance of performance and simplicity when repeatedly selecting the minimum cost worker. |
| Two Pointers with Sorted Segments | O(n log n) | O(n) | Useful when you prefer maintaining ordered candidate pools with pointer expansion from both ends. |