返回介绍

solution / 1300-1399 / 1383.Maximum Performance of a Team / README_EN

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

1383. Maximum Performance of a Team

中文文档

Description

You are given two integers n and k and two integer arrays speed and efficiency both of length n. There are n engineers numbered from 1 to n. speed[i] and efficiency[i] represent the speed and efficiency of the ith engineer respectively.

Choose at most k different engineers out of the n engineers to form a team with the maximum performance.

The performance of a team is the sum of its engineers' speeds multiplied by the minimum efficiency among its engineers.

Return _the maximum performance of this team_. Since the answer can be a huge number, return it modulo 109 + 7.

 

Example 1:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
Output: 60
Explanation: 
We have the maximum performance of the team by selecting engineer 2 (with speed=10 and efficiency=4) and engineer 5 (with speed=5 and efficiency=7). That is, performance = (10 + 5) * min(4, 7) = 60.

Example 2:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 3
Output: 68
Explanation:
This is the same example as the first but k = 3. We can select engineer 1, engineer 2 and engineer 5 to get the maximum performance of the team. That is, performance = (2 + 10 + 5) * min(5, 4, 7) = 68.

Example 3:

Input: n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 4
Output: 72

 

Constraints:

  • 1 <= k <= n <= 105
  • speed.length == n
  • efficiency.length == n
  • 1 <= speed[i] <= 105
  • 1 <= efficiency[i] <= 108

Solutions

Solution 1

class Solution:
  def maxPerformance(
    self, n: int, speed: List[int], efficiency: List[int], k: int
  ) -> int:
    t = sorted(zip(speed, efficiency), key=lambda x: -x[1])
    ans = tot = 0
    mod = 10**9 + 7
    h = []
    for s, e in t:
      tot += s
      ans = max(ans, tot * e)
      heappush(h, s)
      if len(h) == k:
        tot -= heappop(h)
    return ans % mod
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
    int[][] t = new int[n][2];
    for (int i = 0; i < n; ++i) {
      t[i] = new int[] {speed[i], efficiency[i]};
    }
    Arrays.sort(t, (a, b) -> b[1] - a[1]);
    PriorityQueue<Integer> q = new PriorityQueue<>();
    long tot = 0;
    long ans = 0;
    for (var x : t) {
      int s = x[0], e = x[1];
      tot += s;
      ans = Math.max(ans, tot * e);
      q.offer(s);
      if (q.size() == k) {
        tot -= q.poll();
      }
    }
    return (int) (ans % MOD);
  }
}
class Solution {
public:
  int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
    vector<pair<int, int>> t(n);
    for (int i = 0; i < n; ++i) t[i] = {-efficiency[i], speed[i]};
    sort(t.begin(), t.end());
    priority_queue<int, vector<int>, greater<int>> q;
    long long ans = 0, tot = 0;
    int mod = 1e9 + 7;
    for (auto& x : t) {
      int s = x.second, e = -x.first;
      tot += s;
      ans = max(ans, tot * e);
      q.push(s);
      if (q.size() == k) {
        tot -= q.top();
        q.pop();
      }
    }
    return (int) (ans % mod);
  }
};
func maxPerformance(n int, speed []int, efficiency []int, k int) int {
  t := make([][]int, n)
  for i, s := range speed {
    t[i] = []int{s, efficiency[i]}
  }
  sort.Slice(t, func(i, j int) bool { return t[i][1] > t[j][1] })
  var mod int = 1e9 + 7
  ans, tot := 0, 0
  pq := hp{}
  for _, x := range t {
    s, e := x[0], x[1]
    tot += s
    ans = max(ans, tot*e)
    heap.Push(&pq, s)
    if pq.Len() == k {
      tot -= heap.Pop(&pq).(int)
    }
  }
  return ans % mod
}

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 和您的相关数据。
    原文