Skip to content

Commit e8db09e

Browse files
committedApr 12, 2025
Add solution #1383
1 parent 9823847 commit e8db09e

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,267 LeetCode solutions in JavaScript
1+
# 1,268 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1054,6 +1054,7 @@
10541054
1380|[Lucky Numbers in a Matrix](./solutions/1380-lucky-numbers-in-a-matrix.js)|Easy|
10551055
1381|[Design a Stack With Increment Operation](./solutions/1381-design-a-stack-with-increment-operation.js)|Medium|
10561056
1382|[Balance a Binary Search Tree](./solutions/1382-balance-a-binary-search-tree.js)|Medium|
1057+
1383|[Maximum Performance of a Team](./solutions/1383-maximum-performance-of-a-team.js)|Hard|
10571058
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
10581059
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10591060
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* 1383. Maximum Performance of a Team
3+
* https://leetcode.com/problems/maximum-performance-of-a-team/
4+
* Difficulty: Hard
5+
*
6+
* You are given two integers n and k and two integer arrays speed and efficiency both of length n.
7+
* There are n engineers numbered from 1 to n. speed[i] and efficiency[i] represent the speed and
8+
* efficiency of the ith engineer respectively.
9+
*
10+
* Choose at most k different engineers out of the n engineers to form a team with the maximum
11+
* performance.
12+
*
13+
* The performance of a team is the sum of its engineers' speeds multiplied by the minimum
14+
* efficiency among its engineers.
15+
*
16+
* Return the maximum performance of this team. Since the answer can be a huge number, return it
17+
* modulo 109 + 7.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @param {number[]} speed
23+
* @param {number[]} efficiency
24+
* @param {number} k
25+
* @return {number}
26+
*/
27+
var maxPerformance = function(n, speed, efficiency, k) {
28+
const engineers = Array.from({ length: n }, (_, i) => [efficiency[i], speed[i]])
29+
.sort((a, b) => b[0] - a[0]);
30+
const speedHeap = [];
31+
let totalSpeed = 0n;
32+
let maxPerf = 0n;
33+
const MOD = 1000000007n;
34+
35+
function insertSpeed(val) {
36+
speedHeap.push(val);
37+
let i = speedHeap.length - 1;
38+
while (i > 0) {
39+
const parent = Math.floor((i - 1) / 2);
40+
if (speedHeap[parent] <= speedHeap[i]) break;
41+
[speedHeap[i], speedHeap[parent]] = [speedHeap[parent], speedHeap[i]];
42+
i = parent;
43+
}
44+
}
45+
46+
function removeMinSpeed() {
47+
const min = speedHeap[0];
48+
speedHeap[0] = speedHeap.pop();
49+
let i = 0;
50+
while (true) {
51+
const left = 2 * i + 1;
52+
const right = 2 * i + 2;
53+
let smallest = i;
54+
if (left < speedHeap.length && speedHeap[left] < speedHeap[smallest]) {
55+
smallest = left;
56+
}
57+
if (right < speedHeap.length && speedHeap[right] < speedHeap[smallest]) {
58+
smallest = right;
59+
}
60+
if (smallest === i) break;
61+
[speedHeap[i], speedHeap[smallest]] = [speedHeap[smallest], speedHeap[i]];
62+
i = smallest;
63+
}
64+
return min;
65+
}
66+
67+
for (const [eff, spd] of engineers) {
68+
insertSpeed(spd);
69+
totalSpeed += BigInt(spd);
70+
71+
if (speedHeap.length > k) {
72+
totalSpeed -= BigInt(removeMinSpeed());
73+
}
74+
75+
maxPerf = maxPerf > totalSpeed * BigInt(eff) ? maxPerf : totalSpeed * BigInt(eff);
76+
}
77+
78+
return Number(maxPerf % MOD);
79+
};

0 commit comments

Comments
 (0)
Please sign in to comment.