leetcode 1383. Maximum Performance of a Team (team's maximum performance)

insert image description here

n engineers, speed array and efficiency array with length n. Select k engineers at most
each time , and take out k corresponding speed and efficiency numbers. performance=sum(k speed) ✖ min(k efficiency) can be understood as k people work together, and the efficiency is calculated according to the slowest person (others have to wait if one link cannot be completed). Find the maximum performance, and the result is modulo 10 9 +7.


Ideas:

Similar to question 2542 ,
the difference is that question 2542 does not need to take mods, and there are fixed k, this question is a maximum of k.

Because it is limited by the minimum efficiency, the elements of speed and efficiency are paired and sorted in descending order of efficiency.
Then each time the larger efficiency is taken out, it also reduces the dimensionality of the problem.
When the number of people selected is >k, you only need to remove the smaller speed, which requires the use of the minimum heap.

Every time an engineer is added or removed, the sum must be updated.

Since the fixed size is no longer k, the performance needs to be calculated every time a combination is taken.

    public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
    
    
        PriorityQueue<Integer> pq = new PriorityQueue<>(k);
        long sum = 0;
        long res = 0;
        final int MOD = 1000000007;
        int[][] SEs = new int[n][2];
        for(int i = 0; i < n; i++) {
    
    
            SEs[i] = new int[]{
    
    speed[i], efficiency[i]};
        }

        Arrays.sort(SEs, (a,b)->(b[1] - a[1]));

        for(int i = 0; i < n; i++) {
    
    
            if(pq.size() >= k) sum -= pq.poll();
            sum += SEs[i][0];
            pq.offer(SEs[i][0]);
            
            res = Math.max(res, (sum * SEs[i][1]));
        }
        return (int)(res%MOD);
    }

Guess you like

Origin blog.csdn.net/level_code/article/details/130845370