Total Cost to Hire K Workers
MediumUpdated: Aug 2, 2025
Practice on:
Problem
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:
- You will run
ksessions and hire exactly one worker in each session. - In each hiring session, choose the worker with the lowest cost from either the first
candidatesworkers or the lastcandidatesworkers. Break the tie by the smallest index. - For example, if
costs = [3,2,7,7,1,2]andcandidates = 2, then in the first hiring session, we will choose the4thworker because they have the lowest cost[_3,2_ ,7,7,_**1** ,2_]. - In the second hiring session, we will choose
1stworker because they have the same lowest cost as4thworker but they have the smallest index[_3,**2**_ ,7,_7,2_]. Please note that the indexing may be changed in the process. - 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 exactlyk workers.
Examples
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 <= 10^51 <= costs[i] <= 10^51 <= k, candidates <= costs.length
Solution
Method 1 – Two Heaps (Priority Queues)
Intuition
Use two min-heaps to always pick the lowest cost from the first and last candidates workers, simulating the hiring process efficiently.
Approach
Maintain two pointers and two heaps for the left and right candidate windows. At each step, pop the smallest from either heap, and refill the heap from the next available index. Repeat for k hires.
Code
C++
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
long long totalCost(vector<int>& costs, int k, int candidates) {
int n = costs.size();
long long res = 0;
int l = 0, r = n - 1;
priority_queue<int, vector<int>, greater<int>> left, right;
for (int i = 0; i < candidates && l <= r; ++i) left.push(costs[l++]);
for (int i = 0; i < candidates && l <= r; ++i) right.push(costs[r--]);
for (int i = 0; i < k; ++i) {
if (left.empty() || (!right.empty() && right.top() < left.top())) {
res += right.top(); right.pop();
if (l <= r) right.push(costs[r--]);
} else {
res += left.top(); left.pop();
if (l <= r) left.push(costs[l++]);
}
}
return res;
}
};
Java
import java.util.*;
class Solution {
public long totalCost(int[] costs, int k, int candidates) {
int n = costs.length, l = 0, r = n - 1;
long res = 0;
PriorityQueue<Integer> left = new PriorityQueue<>();
PriorityQueue<Integer> right = new PriorityQueue<>();
for (int i = 0; i < candidates && l <= r; ++i) left.offer(costs[l++]);
for (int i = 0; i < candidates && l <= r; ++i) right.offer(costs[r--]);
for (int i = 0; i < k; ++i) {
if (left.isEmpty() || (!right.isEmpty() && right.peek() < left.peek())) {
res += right.poll();
if (l <= r) right.offer(costs[r--]);
} else {
res += left.poll();
if (l <= r) left.offer(costs[l++]);
}
}
return res;
}
}
Python
import heapq
from typing import List
class Solution:
def totalCost(self, costs: List[int], k: int, candidates: int) -> int:
n = len(costs)
res = 0
l, r = 0, n - 1
left, right = [], []
for _ in range(candidates):
if l <= r:
heapq.heappush(left, costs[l]); l += 1
for _ in range(candidates):
if l <= r:
heapq.heappush(right, costs[r]); r -= 1
for _ in range(k):
if not left or (right and right[0] < left[0]):
res += heapq.heappop(right)
if l <= r:
heapq.heappush(right, costs[r]); r -= 1
else:
res += heapq.heappop(left)
if l <= r:
heapq.heappush(left, costs[l]); l += 1
return res
Rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
impl Solution {
pub fn total_cost(costs: Vec<i32>, k: i32, candidates: i32) -> i64 {
let n = costs.len();
let mut l = 0;
let mut r = n as i32 - 1;
let mut left = BinaryHeap::new();
let mut right = BinaryHeap::new();
for _ in 0..candidates {
if l <= r {
left.push(Reverse(costs[l as usize]));
l += 1;
}
}
for _ in 0..candidates {
if l <= r {
right.push(Reverse(costs[r as usize]));
r -= 1;
}
}
let mut res = 0i64;
for _ in 0..k {
let pick_left = right.is_empty() || (!left.is_empty() && left.peek() <= right.peek());
if pick_left {
res += left.pop().unwrap().0 as i64;
if l <= r {
left.push(Reverse(costs[l as usize]));
l += 1;
}
} else {
res += right.pop().unwrap().0 as i64;
if l <= r {
right.push(Reverse(costs[r as usize]));
r -= 1;
}
}
}
res
}
}
TypeScript
function totalCost(costs: number[], k: number, candidates: number): number {
const n = costs.length;
let l = 0, r = n - 1, res = 0;
const left: number[] = [], right: number[] = [];
const push = (heap: number[], val: number) => {
heap.push(val);
heap.sort((a, b) => a - b);
};
for (let i = 0; i < candidates && l <= r; ++i) push(left, costs[l++]);
for (let i = 0; i < candidates && l <= r; ++i) push(right, costs[r--]);
for (let i = 0; i < k; ++i) {
if (!left.length || (right.length && right[0] < left[0])) {
res += right.shift()!;
if (l <= r) push(right, costs[r--]);
} else {
res += left.shift()!;
if (l <= r) push(left, costs[l++]);
}
}
return res;
}
Complexity
- ⏰ Time complexity:
O(k log candidates) - 🧺 Space complexity:
O(candidates)