




Sponsored
This approach uses a heap to dynamically maintain workers while iterating over them sorted by wage-to-quality ratio. The goal is to keep the sum of qualities of the selected workers minimal while ensuring all conditions are satisfied. We sort all workers by their wage-to-quality ratio because for each worker, to satisfy both their minimum wage and relative payment constraints, each selected worker must be paid at least this ratio times their quality.
Time Complexity: O(n log n) due to sorting, and O(n log k) for heap operations.
Space Complexity: O(n) for the sorted list of workers or the heap.
1var mincostToHireWorkers = function(quality, wage, k) {
2    const workers = quality.map((q, i) => [wage[i] / q, q]);
3    workers.sort((a, b) => a[0] - b[0]);
4
5    const maxHeap = new MaxHeap();
6    let qualitySum = 0;
7    let minCost = Infinity;
8
9    for (let [ratio, q] of workers) {
10        qualitySum += q;
11        maxHeap.add(q);
12
13        if (maxHeap.size() > k) {
14            qualitySum -= maxHeap.extract();
15        }
16
17        if (maxHeap.size() === k) {
18            minCost = Math.min(minCost, qualitySum * ratio);
19        }
20    }
21
22    return minCost;
23};
24
25class MaxHeap {
26    constructor() {
27        this.heap = [];
28    }
29
30    size() { return this.heap.length; }
31
32    add(val) {
33        this.heap.push(val);
34        this.bubbleUp();
35    }
36
37    extract() {
38        if (this.size() === 0) return null;
39        this.swap(0, this.size() - 1);
40        const max = this.heap.pop();
41        this.bubbleDown();
42        return max;
43    }
44
45    bubbleUp() {
46        let index = this.size() - 1;
47        while (index > 0) {
48            let element = this.heap[index],
49                parentIndex = Math.floor((index - 1) / 2),
50                parent = this.heap[parentIndex];
51
52            if (parent >= element) break;
53
54            this.swap(index, parentIndex);
55            index = parentIndex;
56        }
57    }
58
59    bubbleDown() {
60        let index = 0;
61        const length = this.size();
62
63        while (true) {
64            let leftChildIndex = 2 * index + 1,
65                rightChildIndex = 2 * index + 2,
66                leftChild, rightChild;
67            let swap = null;
68
69            if (leftChildIndex < length) {
70                leftChild = this.heap[leftChildIndex];
71                if (leftChild > this.heap[index]) swap = leftChildIndex;
72            }
73
74            if (rightChildIndex < length) {
75                rightChild = this.heap[rightChildIndex];
76                if ((swap === null && rightChild > this.heap[index]) || (swap !== null && rightChild > leftChild)) swap = rightChildIndex;
77            }
78
79            if (swap === null) break;
80            this.swap(index, swap);
81            index = swap;
82        }
83    }
84
85    swap(i, j) {
86        [this.heap[i], this.heap[j]] = [this.heap[j], this.heap[i]];
87    }
88}The JavaScript solution utilizes a custom MaxHeap class for managing the highest qualities while iterating over workers sorted by their wage-to-quality ratio. Through leveraging a heap, decisions on worker inclusion make use of quality to keep payment minimized across the evaluated k-group configurations.
This approach focuses on sorting workers by the ratio of their wage expectation to quality. By calculating this ratio, we can use it as a reference point to determine the required payment for every possible group of k workers. Selecting the right workers and calculating the minimum payment by understanding proportional wages can be done by iterating over sorted worker lists using two indices, capturing the required details and performing the necessary calculations.
Time Complexity: O(n log n) due to sorting, and O(n log n) due to managing heap.
Space Complexity: O(n) for worker list and heap storage.
Segregating worker efficiency by respective wage-to-quality metrics solidifies the C algorithm, with simplifying sorting. Equally distributing qualities through heap operations, the smallest worker groups reach the optimally minimized cost using prearranged calculations.