Skip to content

Commit 9798d00

Browse files
committed
feat: add solutions to lc problem: No.3767
1 parent a724505 commit 9798d00

File tree

7 files changed

+407
-8
lines changed

7 files changed

+407
-8
lines changed

solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README.md

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,168 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3767.Ma
9090

9191
<!-- solution:start -->
9292

93-
### 方法一
93+
### 方法一:贪心 + 排序
94+
95+
我们可以先将所有任务都分配给技巧 2,因此初始总分数为 $\sum_{i=0}^{n-1} technique2[i]$。
96+
97+
然后,我们计算每个任务如果改为使用技巧 1 完成所能增加的分数,记为 $\text{diff}[i] = technique1[i] - technique2[i]$。我们将其按照从大到小排序,得到任务索引的排序数组 $\text{idx}$。
98+
99+
接下来,我们选择前 $k$ 个任务使用技巧 1 完成,并将它们的分数差值加到总分数中。对于剩余的任务,如果某个任务使用技巧 1 完成能够增加分数(即 $\text{diff}[i] \geq 0$),我们也将其选择为使用技巧 1 完成。
100+
101+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是任务的数量。
94102

95103
<!-- tabs:start -->
96104

97105
#### Python3
98106

99107
```python
100-
108+
class Solution:
109+
def maxPoints(self, technique1: List[int], technique2: List[int], k: int) -> int:
110+
n = len(technique1)
111+
idx = sorted(range(n), key=lambda i: -(technique1[i] - technique2[i]))
112+
ans = sum(technique2)
113+
for i in idx[:k]:
114+
ans -= technique2[i]
115+
ans += technique1[i]
116+
for i in idx[k:]:
117+
if technique1[i] >= technique2[i]:
118+
ans -= technique2[i]
119+
ans += technique1[i]
120+
return ans
101121
```
102122

103123
#### Java
104124

105125
```java
106-
126+
class Solution {
127+
public long maxPoints(int[] technique1, int[] technique2, int k) {
128+
int n = technique1.length;
129+
Integer[] idx = new Integer[n];
130+
Arrays.setAll(idx, i -> i);
131+
Arrays.sort(idx, (i, j) -> technique1[j] - technique2[j] - (technique1[i] - technique2[i]));
132+
long ans = 0;
133+
for (int x : technique2) {
134+
ans += x;
135+
}
136+
for (int i = 0; i < k; i++) {
137+
int index = idx[i];
138+
ans -= technique2[index];
139+
ans += technique1[index];
140+
}
141+
for (int i = k; i < n; i++) {
142+
int index = idx[i];
143+
if (technique1[index] >= technique2[index]) {
144+
ans -= technique2[index];
145+
ans += technique1[index];
146+
}
147+
}
148+
return ans;
149+
}
150+
}
107151
```
108152

109153
#### C++
110154

111155
```cpp
112-
156+
class Solution {
157+
public:
158+
long long maxPoints(vector<int>& technique1, vector<int>& technique2, int k) {
159+
int n = technique1.size();
160+
vector<int> idx(n);
161+
iota(idx.begin(), idx.end(), 0);
162+
163+
sort(idx.begin(), idx.end(), [&](int i, int j) {
164+
return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]);
165+
});
166+
167+
long long ans = 0;
168+
for (int x : technique2) {
169+
ans += x;
170+
}
171+
172+
for (int i = 0; i < k; i++) {
173+
int index = idx[i];
174+
ans -= technique2[index];
175+
ans += technique1[index];
176+
}
177+
178+
for (int i = k; i < n; i++) {
179+
int index = idx[i];
180+
if (technique1[index] >= technique2[index]) {
181+
ans -= technique2[index];
182+
ans += technique1[index];
183+
}
184+
}
185+
186+
return ans;
187+
}
188+
};
113189
```
114190

115191
#### Go
116192

117193
```go
194+
func maxPoints(technique1 []int, technique2 []int, k int) int64 {
195+
n := len(technique1)
196+
idx := make([]int, n)
197+
for i := 0; i < n; i++ {
198+
idx[i] = i
199+
}
200+
201+
sort.Slice(idx, func(i, j int) bool {
202+
return technique1[idx[j]]-technique2[idx[j]] < technique1[idx[i]]-technique2[idx[i]]
203+
})
204+
205+
var ans int64
206+
for _, x := range technique2 {
207+
ans += int64(x)
208+
}
209+
210+
for i := 0; i < k; i++ {
211+
index := idx[i]
212+
ans -= int64(technique2[index])
213+
ans += int64(technique1[index])
214+
}
215+
216+
for i := k; i < n; i++ {
217+
index := idx[i]
218+
if technique1[index] >= technique2[index] {
219+
ans -= int64(technique2[index])
220+
ans += int64(technique1[index])
221+
}
222+
}
223+
224+
return ans
225+
}
226+
```
227+
228+
#### TypeScript
229+
230+
```ts
231+
function maxPoints(technique1: number[], technique2: number[], k: number): number {
232+
const n = technique1.length;
233+
const idx = Array.from({ length: n }, (_, i) => i);
234+
235+
idx.sort((i, j) => technique1[j] - technique2[j] - (technique1[i] - technique2[i]));
236+
237+
let ans = technique2.reduce((sum, x) => sum + x, 0);
238+
239+
for (let i = 0; i < k; i++) {
240+
const index = idx[i];
241+
ans -= technique2[index];
242+
ans += technique1[index];
243+
}
244+
245+
for (let i = k; i < n; i++) {
246+
const index = idx[i];
247+
if (technique1[index] >= technique2[index]) {
248+
ans -= technique2[index];
249+
ans += technique1[index];
250+
}
251+
}
118252

253+
return ans;
254+
}
119255
```
120256

121257
<!-- tabs:end -->

solution/3700-3799/3767.Maximize Points After Choosing K Tasks/README_EN.md

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,168 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3767.Ma
8888

8989
<!-- solution:start -->
9090

91-
### Solution 1
91+
### Solution 1: Greedy + Sorting
92+
93+
We can first assign all tasks to technique 2, so the initial total score is $\sum_{i=0}^{n-1} technique2[i]$.
94+
95+
Then, we calculate the score increase for each task if it were completed using technique 1 instead, denoted as $\text{diff}[i] = technique1[i] - technique2[i]$. We sort this in descending order to obtain a sorted array of task indices $\text{idx}$.
96+
97+
Next, we select the first $k$ tasks to be completed using technique 1 and add their score differences to the total score. For the remaining tasks, if a task can increase the score by using technique 1 (i.e., $\text{diff}[i] \geq 0$), we also choose to complete it using technique 1.
98+
99+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of tasks.
92100

93101
<!-- tabs:start -->
94102

95103
#### Python3
96104

97105
```python
98-
106+
class Solution:
107+
def maxPoints(self, technique1: List[int], technique2: List[int], k: int) -> int:
108+
n = len(technique1)
109+
idx = sorted(range(n), key=lambda i: -(technique1[i] - technique2[i]))
110+
ans = sum(technique2)
111+
for i in idx[:k]:
112+
ans -= technique2[i]
113+
ans += technique1[i]
114+
for i in idx[k:]:
115+
if technique1[i] >= technique2[i]:
116+
ans -= technique2[i]
117+
ans += technique1[i]
118+
return ans
99119
```
100120

101121
#### Java
102122

103123
```java
104-
124+
class Solution {
125+
public long maxPoints(int[] technique1, int[] technique2, int k) {
126+
int n = technique1.length;
127+
Integer[] idx = new Integer[n];
128+
Arrays.setAll(idx, i -> i);
129+
Arrays.sort(idx, (i, j) -> technique1[j] - technique2[j] - (technique1[i] - technique2[i]));
130+
long ans = 0;
131+
for (int x : technique2) {
132+
ans += x;
133+
}
134+
for (int i = 0; i < k; i++) {
135+
int index = idx[i];
136+
ans -= technique2[index];
137+
ans += technique1[index];
138+
}
139+
for (int i = k; i < n; i++) {
140+
int index = idx[i];
141+
if (technique1[index] >= technique2[index]) {
142+
ans -= technique2[index];
143+
ans += technique1[index];
144+
}
145+
}
146+
return ans;
147+
}
148+
}
105149
```
106150

107151
#### C++
108152

109153
```cpp
110-
154+
class Solution {
155+
public:
156+
long long maxPoints(vector<int>& technique1, vector<int>& technique2, int k) {
157+
int n = technique1.size();
158+
vector<int> idx(n);
159+
iota(idx.begin(), idx.end(), 0);
160+
161+
sort(idx.begin(), idx.end(), [&](int i, int j) {
162+
return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]);
163+
});
164+
165+
long long ans = 0;
166+
for (int x : technique2) {
167+
ans += x;
168+
}
169+
170+
for (int i = 0; i < k; i++) {
171+
int index = idx[i];
172+
ans -= technique2[index];
173+
ans += technique1[index];
174+
}
175+
176+
for (int i = k; i < n; i++) {
177+
int index = idx[i];
178+
if (technique1[index] >= technique2[index]) {
179+
ans -= technique2[index];
180+
ans += technique1[index];
181+
}
182+
}
183+
184+
return ans;
185+
}
186+
};
111187
```
112188

113189
#### Go
114190

115191
```go
192+
func maxPoints(technique1 []int, technique2 []int, k int) int64 {
193+
n := len(technique1)
194+
idx := make([]int, n)
195+
for i := 0; i < n; i++ {
196+
idx[i] = i
197+
}
198+
199+
sort.Slice(idx, func(i, j int) bool {
200+
return technique1[idx[j]]-technique2[idx[j]] < technique1[idx[i]]-technique2[idx[i]]
201+
})
202+
203+
var ans int64
204+
for _, x := range technique2 {
205+
ans += int64(x)
206+
}
207+
208+
for i := 0; i < k; i++ {
209+
index := idx[i]
210+
ans -= int64(technique2[index])
211+
ans += int64(technique1[index])
212+
}
213+
214+
for i := k; i < n; i++ {
215+
index := idx[i]
216+
if technique1[index] >= technique2[index] {
217+
ans -= int64(technique2[index])
218+
ans += int64(technique1[index])
219+
}
220+
}
221+
222+
return ans
223+
}
224+
```
225+
226+
#### TypeScript
227+
228+
```ts
229+
function maxPoints(technique1: number[], technique2: number[], k: number): number {
230+
const n = technique1.length;
231+
const idx = Array.from({ length: n }, (_, i) => i);
232+
233+
idx.sort((i, j) => technique1[j] - technique2[j] - (technique1[i] - technique2[i]));
234+
235+
let ans = technique2.reduce((sum, x) => sum + x, 0);
236+
237+
for (let i = 0; i < k; i++) {
238+
const index = idx[i];
239+
ans -= technique2[index];
240+
ans += technique1[index];
241+
}
242+
243+
for (let i = k; i < n; i++) {
244+
const index = idx[i];
245+
if (technique1[index] >= technique2[index]) {
246+
ans -= technique2[index];
247+
ans += technique1[index];
248+
}
249+
}
116250

251+
return ans;
252+
}
117253
```
118254

119255
<!-- tabs:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
long long maxPoints(vector<int>& technique1, vector<int>& technique2, int k) {
4+
int n = technique1.size();
5+
vector<int> idx(n);
6+
iota(idx.begin(), idx.end(), 0);
7+
8+
sort(idx.begin(), idx.end(), [&](int i, int j) {
9+
return (technique1[j] - technique2[j]) < (technique1[i] - technique2[i]);
10+
});
11+
12+
long long ans = 0;
13+
for (int x : technique2) {
14+
ans += x;
15+
}
16+
17+
for (int i = 0; i < k; i++) {
18+
int index = idx[i];
19+
ans -= technique2[index];
20+
ans += technique1[index];
21+
}
22+
23+
for (int i = k; i < n; i++) {
24+
int index = idx[i];
25+
if (technique1[index] >= technique2[index]) {
26+
ans -= technique2[index];
27+
ans += technique1[index];
28+
}
29+
}
30+
31+
return ans;
32+
}
33+
};

0 commit comments

Comments
 (0)