Advertisement
exmkg

Untitled

Oct 27th, 2024
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. class Solution {
  2.     public double mincostToHireWorkers(int[] q, int[] w, int K) {
  3.         double[][] workers = new double[q.length][2];
  4.         for (int i = 0; i < q.length; i++) {
  5.             workers[i] = new double[]{
  6.                 w[i] * 1.0 / q[i],
  7.                 q[i] * 1.0
  8.             };
  9.         }
  10.         Arrays.sort(workers, (a, b) -> Double.compare(a[0], b[0]));
  11.         double res = Double.MAX_VALUE;
  12.         double qsum = 0; // the sum of the lowest k qualities
  13.         PriorityQueue<Double> pq = new PriorityQueue<>(Collections.reverseOrder());
  14.         for (double[] worker : workers) {
  15.             qsum += worker[1];
  16.             pq.add(worker[1]);
  17.  
  18.             // Remove highest quality worker to minize price
  19.             if (pq.size() == K + 1) qsum -= pq.poll();
  20.             // total quality of k workers * highest wage/quality ratio = total pay for that group of k workers
  21.             if (pq.size() == K) res = Math.min(res, qsum * worker[0]);
  22.         }
  23.         return res;
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement