Skip to content

Commit 419b362

Browse files
committed
feat: add solutions to lc problem: No.1383
No.1383.Maximum Performance of a Team
1 parent 2b89a6a commit 419b362

File tree

6 files changed

+202
-3
lines changed

6 files changed

+202
-3
lines changed

solution/1300-1399/1383.Maximum Performance of a Team/README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,92 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
**方法一:优先队列**
55+
56+
本题是求“速度和”与“效率最小值”乘积的最大值。变量有两个,我们可以从大到小枚举 `efficiency[i]` 作为效率最小值,在所有效率大于等于 `efficiency[i]` 的工程师中选取不超过 k-1 个,让他们速度和最大。
57+
5458
<!-- tabs:start -->
5559

5660
### **Python3**
5761

5862
<!-- 这里可写当前语言的特殊实现逻辑 -->
5963

6064
```python
61-
65+
class Solution:
66+
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
67+
team = [(s, e) for s, e in zip(speed, efficiency)]
68+
team.sort(key=lambda x: -x[1])
69+
q = []
70+
t = 0
71+
ans = 0
72+
mod = int(1e9) + 7
73+
for s, e in team:
74+
t += s
75+
ans = max(ans, t * e)
76+
heappush(q, s)
77+
if len(q) >= k:
78+
t -= heappop(q)
79+
return ans % mod
6280
```
6381

6482
### **Java**
6583

6684
<!-- 这里可写当前语言的特殊实现逻辑 -->
6785

6886
```java
87+
class Solution {
88+
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
89+
int[][] team = new int[n][2];
90+
for (int i = 0; i < n; ++i) {
91+
team[i] = new int[]{speed[i], efficiency[i]};
92+
}
93+
Arrays.sort(team, (a, b) -> b[1] - a[1]);
94+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> a - b);
95+
long t = 0;
96+
long ans = 0;
97+
int mod = (int) 1e9 + 7;
98+
for (int[] x : team) {
99+
int s = x[0], e = x[1];
100+
t += s;
101+
ans = Math.max(ans, t * e);
102+
q.offer(s);
103+
if (q.size() >= k) {
104+
t -= q.poll();
105+
}
106+
}
107+
return (int) (ans % mod);
108+
}
109+
}
110+
```
69111

112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
118+
vector<pair<int, int>> team;
119+
for (int i = 0; i < n; ++i) team.push_back({-efficiency[i], speed[i]});
120+
sort(team.begin(), team.end());
121+
priority_queue<int, vector<int>, greater<int>> q;
122+
long long ans = 0;
123+
int mod = 1e9 + 7;
124+
long long t = 0;
125+
for (auto& x : team)
126+
{
127+
int s = x.second, e = -x.first;
128+
t += s;
129+
ans = max(ans, e * t);
130+
q.push(s);
131+
if (q.size() >= k)
132+
{
133+
t -= q.top();
134+
q.pop();
135+
}
136+
}
137+
return (int) (ans % mod);
138+
}
139+
};
70140
```
71141
72142
### **...**

solution/1300-1399/1383.Maximum Performance of a Team/README_EN.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,79 @@ We have the maximum performance of the team by selecting engineer 2 (with speed=
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
61+
team = [(s, e) for s, e in zip(speed, efficiency)]
62+
team.sort(key=lambda x: -x[1])
63+
q = []
64+
t = 0
65+
ans = 0
66+
mod = int(1e9) + 7
67+
for s, e in team:
68+
t += s
69+
ans = max(ans, t * e)
70+
heappush(q, s)
71+
if len(q) >= k:
72+
t -= heappop(q)
73+
return ans % mod
6074
```
6175

6276
### **Java**
6377

6478
```java
79+
class Solution {
80+
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
81+
int[][] team = new int[n][2];
82+
for (int i = 0; i < n; ++i) {
83+
team[i] = new int[]{speed[i], efficiency[i]};
84+
}
85+
Arrays.sort(team, (a, b) -> b[1] - a[1]);
86+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> a - b);
87+
long t = 0;
88+
long ans = 0;
89+
int mod = (int) 1e9 + 7;
90+
for (int[] x : team) {
91+
int s = x[0], e = x[1];
92+
t += s;
93+
ans = Math.max(ans, t * e);
94+
q.offer(s);
95+
if (q.size() >= k) {
96+
t -= q.poll();
97+
}
98+
}
99+
return (int) (ans % mod);
100+
}
101+
}
102+
```
65103

104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
110+
vector<pair<int, int>> team;
111+
for (int i = 0; i < n; ++i) team.push_back({-efficiency[i], speed[i]});
112+
sort(team.begin(), team.end());
113+
priority_queue<int, vector<int>, greater<int>> q;
114+
long long ans = 0;
115+
int mod = 1e9 + 7;
116+
long long t = 0;
117+
for (auto& x : team)
118+
{
119+
int s = x.second, e = -x.first;
120+
t += s;
121+
ans = max(ans, e * t);
122+
q.push(s);
123+
if (q.size() >= k)
124+
{
125+
t -= q.top();
126+
q.pop();
127+
}
128+
}
129+
return (int) (ans % mod);
130+
}
131+
};
66132
```
67133
68134
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
4+
vector<pair<int, int>> team;
5+
for (int i = 0; i < n; ++i) team.push_back({-efficiency[i], speed[i]});
6+
sort(team.begin(), team.end());
7+
priority_queue<int, vector<int>, greater<int>> q;
8+
long long ans = 0;
9+
int mod = 1e9 + 7;
10+
long long t = 0;
11+
for (auto& x : team)
12+
{
13+
int s = x.second, e = -x.first;
14+
t += s;
15+
ans = max(ans, e * t);
16+
q.push(s);
17+
if (q.size() >= k)
18+
{
19+
t -= q.top();
20+
q.pop();
21+
}
22+
}
23+
return (int) (ans % mod);
24+
}
25+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int maxPerformance(int n, int[] speed, int[] efficiency, int k) {
3+
int[][] team = new int[n][2];
4+
for (int i = 0; i < n; ++i) {
5+
team[i] = new int[]{speed[i], efficiency[i]};
6+
}
7+
Arrays.sort(team, (a, b) -> b[1] - a[1]);
8+
PriorityQueue<Integer> q = new PriorityQueue<>((a, b) -> a - b);
9+
long t = 0;
10+
long ans = 0;
11+
int mod = (int) 1e9 + 7;
12+
for (int[] x : team) {
13+
int s = x[0], e = x[1];
14+
t += s;
15+
ans = Math.max(ans, t * e);
16+
q.offer(s);
17+
if (q.size() >= k) {
18+
t -= q.poll();
19+
}
20+
}
21+
return (int) (ans % mod);
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxPerformance(self, n: int, speed: List[int], efficiency: List[int], k: int) -> int:
3+
team = [(s, e) for s, e in zip(speed, efficiency)]
4+
team.sort(key=lambda x: -x[1])
5+
q = []
6+
t = 0
7+
ans = 0
8+
mod = int(1e9) + 7
9+
for s, e in team:
10+
t += s
11+
ans = max(ans, t * e)
12+
heappush(q, s)
13+
if len(q) >= k:
14+
t -= heappop(q)
15+
return ans % mod

solution/1300-1399/1386.Cinema Seat Allocation/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
哈希表中没有出现的行,每行可坐 2 个 4 人家庭,即 `(n - len(m)) << 1`
6262

63-
遍历哈希表中出现的行,依次尝试 1234, 5678, 3456 这几个连续座位(注意这里下标从 0 开始)是否均没被预定,是则累加答案,并且将此连续作为置为已预定
63+
遍历哈希表中出现的行,依次贪心尝试 1234, 5678, 3456 这几个连续座位(注意这里下标从 0 开始)是否均没被预定,是则累加答案,并且将此连续座位置为已预定
6464

6565
<!-- tabs:start -->
6666

0 commit comments

Comments
 (0)