[W] A008_ heap maximum value of team performance (top small pile)

One, Title Description

There are n engineers numbered from 1 to n and two arrays: speed and efficiency, where speed[i] and efficiency[i] represent the speed and efficiency for the i-th engineer respectively. Return the maximum performance of a team composed of at most k engineers, since the answer can be a huge number, return this modulo 10^9 + 7.

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

输入:n = 6, speed = [2,10,3,1,5,8], efficiency = [5,4,3,9,7,2], k = 2
输出:60
解释:
我们选择工程师 2(speed=10 且 efficiency=4)和工程师 5(speed=5 且 efficiency=7)。
他们的团队表现值为 performance = (10 + 5) * min(4, 7) = 60

Meaning of the questions: When two solutions of equal efficiency, speed and greater scheme of apparently better. Transformed into question how quickly obtain optimal efficiency of each program.

algorithm

  • Everyone in descending order according to the Efficiency e.
  • Select k largest speed in an array of species.
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
  int[][] pair = new int[n][2];
  for (int i = 0; i < n; i++) {
      pair[i][0] = speed[i];
      pair[i][1] = efficiency[i];
  }
  Arrays.sort(pair, (e1, e2) -> e2[1] - e1[1]);
  
  PriorityQueue<Integer> pQ = new PriorityQueue<>();
  long max = 0, sd = 0;
  for (int i = 0; i < n; i++) {
      pQ.add(pair[i][0]);
      sd += pair[i][0];
      if (pQ.size() > k) {
          sd -= pQ.poll();
      }
      max = Math.max(max, sd * pair[i][1]);
  }
  return (int)(max % ((int)1e9 + 7));
}

Complexity Analysis

  • time complexity: O ( n l O g n ) O(n log n) ,
  • Space complexity: O ( k ) O(k)
Published 495 original articles · won praise 105 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_43539599/article/details/104880448