#1383
Hard Algorithms Maximum performance of a team
Array Greedy Sorting Heap (Priority Queue)
47.7% acceptance
Feb 25, 2026
3204
85
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.
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn max_performance(_n: i32, speed: Vec<i32>, efficiency: Vec<i32>, k: i32) -> i32 {
use std::collections::BinaryHeap;
use std::cmp::Reverse;
const MOD: i64 = 1_000_000_007;
let k = k as usize;
let mut eng: Vec<(i32, i32)> = efficiency.into_iter().zip(speed.into_iter()).collect();
eng.sort_unstable_by(|a, b| b.0.cmp(&a.0)); // descending efficiency
let mut heap: BinaryHeap<Reverse<i32>> = BinaryHeap::new(); // min-heap of speeds
let mut speed_sum: i64 = 0;
let mut ans: i64 = 0;
for (eff, spd) in eng {
heap.push(Reverse(spd));
speed_sum += spd as i64;
if heap.len() > k {
let Reverse(min_spd) = heap.pop().unwrap();
speed_sum -= min_spd as i64;
}
ans = ans.max(speed_sum * eff as i64);
}
(ans % MOD) as i32
}
}