返回介绍

solution / 0800-0899 / 0857.Minimum Cost to Hire K Workers / README_EN

发布于 2024-06-17 01:03:33 字数 4975 浏览 0 评论 0 收藏 0

857. Minimum Cost to Hire K Workers

中文文档

Description

There are n workers. You are given two integer arrays quality and wage where quality[i] is the quality of the ith worker and wage[i] is the minimum wage expectation for the 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:

  1. Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group.
  2. Every worker in the paid group must be paid at least their minimum wage expectation.

Given the integer k, return _the least amount of money needed to form a paid group satisfying the above conditions_. Answers within 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 0th worker and 35 to 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 0th worker, 13.33333 to 2nd and 3rd workers separately.

 

Constraints:

  • n == quality.length == wage.length
  • 1 <= k <= n <= 104
  • 1 <= quality[i], wage[i] <= 104

Solutions

Solution 1

class Solution:
  def mincostToHireWorkers(
    self, quality: List[int], wage: List[int], k: int
  ) -> float:
    t = sorted(zip(quality, wage), key=lambda x: x[1] / x[0])
    ans, tot = inf, 0
    h = []
    for q, w in t:
      tot += q
      heappush(h, -q)
      if len(h) == k:
        ans = min(ans, w / q * tot)
        tot += heappop(h)
    return ans
class Solution {
  public double mincostToHireWorkers(int[] quality, int[] wage, int k) {
    int n = quality.length;
    Pair[] t = new Pair[n];
    for (int i = 0; i < n; ++i) {
      t[i] = new Pair(quality[i], wage[i]);
    }
    Arrays.sort(t, (a, b) -> Double.compare(a.x, b.x));
    PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
    double ans = 1e9;
    int tot = 0;
    for (var e : t) {
      tot += e.q;
      pq.offer(e.q);
      if (pq.size() == k) {
        ans = Math.min(ans, tot * e.x);
        tot -= pq.poll();
      }
    }
    return ans;
  }
}

class Pair {
  double x;
  int q;

  Pair(int q, int w) {
    this.q = q;
    this.x = (double) w / q;
  }
}
class Solution {
public:
  double mincostToHireWorkers(vector<int>& quality, vector<int>& wage, int k) {
    int n = quality.size();
    vector<pair<double, int>> t(n);
    for (int i = 0; i < n; ++i) {
      t[i] = {(double) wage[i] / quality[i], quality[i]};
    }
    sort(t.begin(), t.end());
    priority_queue<int> pq;
    double ans = 1e9;
    int tot = 0;
    for (auto& [x, q] : t) {
      tot += q;
      pq.push(q);
      if (pq.size() == k) {
        ans = min(ans, tot * x);
        tot -= pq.top();
        pq.pop();
      }
    }
    return ans;
  }
};
func mincostToHireWorkers(quality []int, wage []int, k int) float64 {
  t := []pair{}
  for i, q := range quality {
    t = append(t, pair{float64(wage[i]) / float64(q), q})
  }
  sort.Slice(t, func(i, j int) bool { return t[i].x < t[j].x })
  tot := 0
  var ans float64 = 1e9
  pq := hp{}
  for _, e := range t {
    tot += e.q
    heap.Push(&pq, e.q)
    if pq.Len() == k {
      ans = min(ans, float64(tot)*e.x)
      tot -= heap.Pop(&pq).(int)
    }
  }
  return ans
}

type pair struct {
  x float64
  q int
}

type hp struct{ sort.IntSlice }

func (h *hp) Push(v any) { h.IntSlice = append(h.IntSlice, v.(int)) }
func (h *hp) Pop() any {
  a := h.IntSlice
  v := a[len(a)-1]
  h.IntSlice = a[:len(a)-1]
  return v
}
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文