[LeetCode] 2462. Total Cost to Hire K Workers

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 last candidates workers. Break the tie by the smallest index.
    • For example, if 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].
    • In the second hiring session, we will choose 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.
  • 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 位工人的总代价。

给你一个下标从 0 开始的整数数组 costs ,其中 costs[i] 是雇佣第 i 位工人的代价。

同时给你两个整数 k 和 candidates 。我们想根据以下规则恰好雇佣 k 位工人:

总共进行 k 轮雇佣,且每一轮恰好雇佣一位工人。
在每一轮雇佣中,从最前面 candidates 和最后面 candidates 人中选出代价最小的一位工人,如果有多位代价相同且最小的工人,选择下标更小的一位工人。
比方说,costs = [3,2,7,7,1,2] 且 candidates = 2 ,第一轮雇佣中,我们选择第 4 位工人,因为他的代价最小 [3,2,7,7,1,2] 。
第二轮雇佣,我们选择第 1 位工人,因为他们的代价与第 4 位工人一样都是最小代价,而且下标更小,[3,2,7,7,2] 。注意每一轮雇佣后,剩余工人的下标可能会发生变化。
如果剩余员工数目不足 candidates 人,那么下一轮雇佣他们中代价最小的一人,如果有多位代价相同且最小的工人,选择下标更小的一位工人。
一位工人只能被选择一次。
返回雇佣恰好 k 位工人的总代价。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/total-cost-to-hire-k-workers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是贪心。题目说的有点绕,这里我用一个例子重新说明一下。题目给了一个 costs 数组,代表工人们的工价。我们需要从中挑出 k 个工人,使得他们的工价最低;同时挑选的范围是数组左右两边范围在 candidates 之内的工人才可以被挑选。举个例子,比如 candidates = 2,那么只有蓝色部分的元素才是可以被挑选的。也就是说只有数组左侧 candidates 个元素和右侧 candidates 个元素是可以被挑选的。

[3,2,7,7,1,2]

因为蓝色部分的元素有多个,所以这里我们需要用两个最小堆分别存储左侧的蓝色元素和右侧的蓝色元素,这样每次挑选工人的时候我们就能得到工价最小的那个了。

时间O(nlogc) - c is candidates

空间O(n)

Java实现

 1 class Solution {
 2     public long totalCost(int[] costs, int k, int candidates) {
 3         int n = costs.length;
 4         PriorityQueue<Integer> queue1 = new PriorityQueue<>();
 5         PriorityQueue<Integer> queue2 = new PriorityQueue<>();
 6         
 7         int i = 0;
 8         int j = n - 1;
 9         long res = 0;
10         while (k-- > 0) {
11             while (queue1.size() < candidates && i <= j) {
12                 queue1.offer(costs[i++]);
13             }
14             while (queue2.size() < candidates && i <= j) {
15                 queue2.offer(costs[j--]);
16             }
17 
18             int left = queue1.size() > 0 ? queue1.peek() : Integer.MAX_VALUE;
19             int right = queue2.size() > 0 ? queue2.peek() : Integer.MAX_VALUE;
20             if (left <= right) {
21                 res += left;
22                 queue1.poll();
23             } else {
24                 res += right;
25                 queue2.poll();
26             }
27         }
28         return res;
29     }
30 }

 

LeetCode 题目总结

posted @ 2023-06-27 11:13  CNoodle  阅读(117)  评论(0编辑  收藏  举报