comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
Medium |
1763 |
Weekly Contest 318 Q3 |
|
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
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 lastcandidates
workers. 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 the4th
worker because they have the lowest cost[3,2,7,7,1,2]
. - In the second hiring session, we will choose
1st
worker because they have the same lowest cost as4th
worker but they have the smallest index[3,2,7,7,2]
. Please note that the indexing may be changed in the process.
- For example, if
- 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.
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] <= 105
1 <= k, candidates <= costs.length
First, we check if
Otherwise, we use a min heap
We first add the costs and corresponding indices of the first
Then we perform
After the loop ends, we return the answer.
The time complexity is
class Solution:
def totalCost(self, costs: List[int], k: int, candidates: int) -> int:
n = len(costs)
if candidates * 2 >= n:
return sum(sorted(costs)[:k])
pq = []
for i, c in enumerate(costs[:candidates]):
heappush(pq, (c, i))
for i in range(n - candidates, n):
heappush(pq, (costs[i], i))
heapify(pq)
l, r = candidates, n - candidates - 1
ans = 0
for _ in range(k):
c, i = heappop(pq)
ans += c
if l > r:
continue
if i < l:
heappush(pq, (costs[l], l))
l += 1
else:
heappush(pq, (costs[r], r))
r -= 1
return ans
class Solution {
public long totalCost(int[] costs, int k, int candidates) {
int n = costs.length;
long ans = 0;
if (candidates * 2 >= n) {
Arrays.sort(costs);
for (int i = 0; i < k; ++i) {
ans += costs[i];
}
return ans;
}
PriorityQueue<int[]> pq
= new PriorityQueue<>((a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
for (int i = 0; i < candidates; ++i) {
pq.offer(new int[] {costs[i], i});
pq.offer(new int[] {costs[n - i - 1], n - i - 1});
}
int l = candidates, r = n - candidates - 1;
while (k-- > 0) {
var p = pq.poll();
ans += p[0];
if (l > r) {
continue;
}
if (p[1] < l) {
pq.offer(new int[] {costs[l], l++});
} else {
pq.offer(new int[] {costs[r], r--});
}
}
return ans;
}
}
class Solution {
public:
long long totalCost(vector<int>& costs, int k, int candidates) {
int n = costs.size();
if (candidates * 2 > n) {
sort(costs.begin(), costs.end());
return accumulate(costs.begin(), costs.begin() + k, 0LL);
}
using pii = pair<int, int>;
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int i = 0; i < candidates; ++i) {
pq.emplace(costs[i], i);
pq.emplace(costs[n - i - 1], n - i - 1);
}
long long ans = 0;
int l = candidates, r = n - candidates - 1;
while (k--) {
auto [cost, i] = pq.top();
pq.pop();
ans += cost;
if (l > r) {
continue;
}
if (i < l) {
pq.emplace(costs[l], l++);
} else {
pq.emplace(costs[r], r--);
}
}
return ans;
}
};
func totalCost(costs []int, k int, candidates int) (ans int64) {
n := len(costs)
if candidates*2 > n {
sort.Ints(costs)
for _, x := range costs[:k] {
ans += int64(x)
}
return
}
pq := hp{}
for i, x := range costs[:candidates] {
heap.Push(&pq, pair{x, i})
heap.Push(&pq, pair{costs[n-i-1], n - i - 1})
}
l, r := candidates, n-candidates-1
for ; k > 0; k-- {
p := heap.Pop(&pq).(pair)
ans += int64(p.cost)
if l > r {
continue
}
if p.i < l {
heap.Push(&pq, pair{costs[l], l})
l++
} else {
heap.Push(&pq, pair{costs[r], r})
r--
}
}
return
}
type pair struct{ cost, i int }
type hp []pair
func (h hp) Len() int { return len(h) }
func (h hp) Less(i, j int) bool {
return h[i].cost < h[j].cost || (h[i].cost == h[j].cost && h[i].i < h[j].i)
}
func (h hp) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *hp) Push(v any) { *h = append(*h, v.(pair)) }
func (h *hp) Pop() any { a := *h; v := a[len(a)-1]; *h = a[:len(a)-1]; return v }
function totalCost(costs: number[], k: number, candidates: number): number {
const n = costs.length;
if (candidates * 2 >= n) {
costs.sort((a, b) => a - b);
return costs.slice(0, k).reduce((acc, x) => acc + x, 0);
}
const pq = new PriorityQueue({
compare: (a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]),
});
for (let i = 0; i < candidates; ++i) {
pq.enqueue([costs[i], i]);
pq.enqueue([costs[n - i - 1], n - i - 1]);
}
let [l, r] = [candidates, n - candidates - 1];
let ans = 0;
while (k--) {
const [cost, i] = pq.dequeue()!;
ans += cost;
if (l > r) {
continue;
}
if (i < l) {
pq.enqueue([costs[l], l++]);
} else {
pq.enqueue([costs[r], r--]);
}
}
return ans;
}