LeetCode //C - 857. Minimum Cost to Hire K Workers
This article discusses how to hire K workers at the lowest cost, ensuring wages proportional to work quality and no less than the minimum desired wage. The key idea is to sort workers by their wage/qu
857. Minimum Cost to Hire K Workers
There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the i t h i^{th} ith worker and wage[i] is the minimum wage expectation for the i t h i^{th} ith worker.
We want to hire exactly k workers to form a paid group. To hire a group of k workers, we must pay them according to the following rules:
- Every worker in the paid group must be paid at least their minimum wage expectation.
- In the group, each worker’s pay must be directly proportional to their quality. This means if a worker’s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.
Given the integer k, return the least amount of money needed to form a paid group satisfying the above conditions. Answers within 1 0 − 5 10^{-5} 10−5 of the actual answer will be accepted.
Example 1:
Input: quality = [10,20,5], wage = [70,50,30], k = 2
Output: 105.00000
Explanation: We pay 70 to 0 t h 0^{th} 0th worker and 35 to 2 n d 2^{nd} 2nd worker.
Example 2:
Input: quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
Output: 30.66667
Explanation: We pay 4 to 0 t h 0^{th} 0th worker, 13.33333 to 2 n d 2^{nd} 2nd and 3 r d 3^{rd} 3rd workers separately.
Constraints:
- n == quality.length == wage.length
- 1 < = k < = n < = 1 0 4 1 <= k <= n <= 10^4 1<=k<=n<=104
- 1 < = q u a l i t y [ i ] , w a g e [ i ] < = 1 0 4 1 <= quality[i], wage[i] <= 10^4 1<=quality[i],wage[i]<=104
From: LeetCode
Link: 857. Minimum Cost to Hire K Workers
Solution:
Ideas:
-
Paying proportionally means a group’s pay is fully determined by a single ratio r (pay per quality). For any chosen group, r must be at least every member’s wage[i]/quality[i]. So if we process workers in ascending ratio, the current worker’s ratio is the tightest feasible r for any group that includes them.
-
For each ratio, minimize total pay = r * sum(qualities of chosen k). That’s why we keep the k smallest total quality using a max-heap (pop the largest quality when we exceed k).
Code:
typedef struct {
double ratio; // wage[i] / quality[i]
int q; // quality[i]
} Worker;
static int cmpWorkers(const void* a, const void* b) {
const Worker* wa = (const Worker*)a;
const Worker* wb = (const Worker*)b;
if (wa->ratio < wb->ratio) return -1;
if (wa->ratio > wb->ratio) return 1;
return 0;
}
/* Max-heap for qualities */
typedef struct {
int *data;
int size;
int cap;
} MaxHeap;
static void heapInit(MaxHeap* h, int cap) {
h->data = (int*)malloc(sizeof(int) * cap);
h->size = 0;
h->cap = cap;
}
static void heapSwap(int* a, int* b) {
int t = *a; *a = *b; *b = t;
}
static void heapPush(MaxHeap* h, int val) {
int i = h->size++;
h->data[i] = val;
// up-heap
while (i > 0) {
int p = (i - 1) / 2;
if (h->data[p] >= h->data[i]) break;
heapSwap(&h->data[p], &h->data[i]);
i = p;
}
}
static int heapPop(MaxHeap* h) {
int ret = h->data[0];
h->data[0] = h->data[--h->size];
// down-heap
int i = 0;
while (1) {
int l = 2*i + 1, r = 2*i + 2, largest = i;
if (l < h->size && h->data[l] > h->data[largest]) largest = l;
if (r < h->size && h->data[r] > h->data[largest]) largest = r;
if (largest == i) break;
heapSwap(&h->data[i], &h->data[largest]);
i = largest;
}
return ret;
}
double mincostToHireWorkers(int* quality, int qualitySize, int* wage, int wageSize, int k) {
(void)wageSize; // parameters are guaranteed equal per constraints
int n = qualitySize;
Worker* arr = (Worker*)malloc(sizeof(Worker) * n);
for (int i = 0; i < n; ++i) {
arr[i].ratio = (double)wage[i] / (double)quality[i];
arr[i].q = quality[i];
}
qsort(arr, n, sizeof(Worker), cmpWorkers);
MaxHeap heap;
heapInit(&heap, n);
long long sumQ = 0;
double ans = INFINITY;
for (int i = 0; i < n; ++i) {
heapPush(&heap, arr[i].q);
sumQ += arr[i].q;
if (heap.size > k) {
int removed = heapPop(&heap); // remove largest quality to minimize sum
sumQ -= removed;
}
if (heap.size == k) {
double cost = (double)sumQ * arr[i].ratio;
if (cost < ans) ans = cost;
}
}
free(heap.data);
free(arr);
return ans;
}
更多推荐
所有评论(0)