LeetCode_2553_Total Cost to Hire K Workers (Java)
LeetCode_2553_Total Cost to Hire K Workers (Java)
2553. Total Cost to Hire K Workers
Medium
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
문제 풀이
왼쪽K개중 최솟값, 오른쪽 K개중 최솟값을 O(1)에 찾으며 삽입시 log(N)으로 정렬하는 최소힙 2개를 사용했다. 그리고 서로의 구간을 침범하지 않게 left<=right를 유지했다.
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public long totalCost(int[] costs, int k, int candidates) {
int left, right;
left = 0;
right = costs.length - 1;
PriorityQueue<Integer> left_pq = new PriorityQueue<>();
PriorityQueue<Integer> right_pq = new PriorityQueue<>();
for(int i=0; i<candidates; i++){
if(left <= right) left_pq.offer(costs[left++]);
}
for(int i=0; i<candidates; i++){
if(left <= right) right_pq.offer(costs[right--]);
}
long res=0;
while(k-->0){
int a, b;
if(left_pq.isEmpty()) {
res += right_pq.poll();
continue;
}
else if(right_pq.isEmpty()){
res += left_pq.poll();
continue;
}
a = left_pq.peek();
b = right_pq.peek();
if(a <= b) {
left_pq.poll();
if(left <= right) left_pq.offer(costs[left++]);
res+= a;
}
else if (b < a){
right_pq.poll();
if(left <= right) right_pq.offer(costs[right--]);
res += b;
}
}
return res;
}
}
This post is licensed under
CC BY 4.0
by the author.