Before attempting this problem, you should be comfortable with:
The simplest approach is to try all possible subsets of engineers with size at most k. For each subset, we compute the sum of speeds and multiply by the minimum efficiency in that subset. We track the maximum performance across all valid subsets.
This works because performance is defined as (sum of speeds) * (minimum efficiency), so we need to consider every combination to find the optimal team.
k == 0), stop recursing.class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
MOD = 1000000007
res = 0
def dfs(i, k, speedSum, minEff):
nonlocal res
res = max(res, speedSum * minEff)
if i == n or k == 0:
return
dfs(i + 1, k, speedSum, minEff)
dfs(i + 1, k - 1, speedSum + speed[i], min(minEff, efficiency[i]))
dfs(0, k, 0, float("inf"))
return res % MODThe key insight is that the minimum efficiency in a team acts as a bottleneck. If we fix which engineer has the minimum efficiency, we want to maximize the sum of speeds among the remaining selected engineers.
By sorting engineers in descending order of efficiency, as we iterate through them, each new engineer we consider has the smallest efficiency so far. At that point, we want to pick the engineers with the highest speeds (up to k total) from those we have seen. A min-heap helps us efficiently maintain the top k speeds.
k engineers, remove the one with the smallest speed.class Solution:
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
MOD = 10**9 + 7
eng = sorted(zip(efficiency, speed), reverse=True)
res = speedSum = 0
minHeap = []
for eff, spd in eng:
if len(minHeap) == k:
speedSum -= heapq.heappop(minHeap)
speedSum += spd
heapq.heappush(minHeap, spd)
res = max(res, eff * speedSum)
return res % MODWhere is the number of engineers and is the maximum number of engineers that can be selected.
The performance value is (sum of speeds) * (minimum efficiency), which can exceed the 32-bit integer range for large inputs. Use 64-bit integers (long in Java, long long in C++) for intermediate calculations, and only apply the modulo when returning the final result.
The modulo operation should only be applied to the final answer, not during intermediate maximum comparisons. Applying modulo too early (e.g., res = max(res, performance % MOD)) corrupts the comparison logic because a smaller modulo result might incorrectly appear larger than the true maximum.
The problem asks for at most k engineers, meaning teams of size 1, 2, ..., up to k are all valid. A common mistake is only considering teams of exactly size k. The heap-based solution naturally handles this by computing performance at each step as engineers are added.
When the heap exceeds k engineers, you must remove the engineer with the smallest speed (the top of a min-heap). A common error is using a max-heap instead, which would remove the fastest engineer and yield suboptimal results. The min-heap ensures we always keep the k fastest engineers seen so far.
After removing an engineer from the heap, the speed sum must be decremented by that engineer's speed. Forgetting this update causes the speed sum to be incorrectly inflated, leading to wrong performance calculations.