Skip to content

Commit b0821cc

Browse files
rain84yanglbme
andauthored
feat: add solutions to lc problem: No.0826 (#3123)
Co-authored-by: Libin YANG <[email protected]>
1 parent 76c3531 commit b0821cc

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

solution/0800-0899/0826.Most Profit Assigning Work/README.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,127 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
190190

191191
<!-- solution:end -->
192192

193+
<!-- solution:start -->
194+
195+
### 方法二:动态规划
196+
197+
我们不妨记 $m = \max(\text{difficulty})$,定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示难度小于等于 $i$ 的工作中收益的最大值,初始时 $f[i] = 0$。
198+
199+
然后我们遍历工作,对于每个工作 $(d, p)$,我们更新 $f[d] = \max(f[d], p)$。
200+
201+
接下来,我们从 $1$ 到 $m$ 遍历,对于每个 $i$,我们更新 $f[i] = \max(f[i], f[i - 1])$。
202+
203+
最后,我们遍历工人,对于每个工人 $w$,我们将 $f[\min(w, m)]$ 加到答案中。
204+
205+
时间复杂度 $O(n + M)$,空间复杂度 $O(M)$。其中 $n$ 是数组 `profit` 的长度,而 $M$ 是数组 `difficulty` 中的最大值。
206+
207+
<!-- tabs:start -->
208+
209+
#### Python3
210+
211+
```python
212+
class Solution:
213+
def maxProfitAssignment(
214+
self, difficulty: List[int], profit: List[int], worker: List[int]
215+
) -> int:
216+
m = max(difficulty)
217+
f = [0] * (m + 1)
218+
for d, p in zip(difficulty, profit):
219+
f[d] = max(f[d], p)
220+
for i in range(1, m + 1):
221+
f[i] = max(f[i], f[i - 1])
222+
return sum(f[min(w, m)] for w in worker)
223+
```
224+
225+
#### Java
226+
227+
```java
228+
class Solution {
229+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
230+
int m = Arrays.stream(difficulty).max().getAsInt();
231+
int[] f = new int[m + 1];
232+
int n = profit.length;
233+
for (int i = 0; i < n; ++i) {
234+
int d = difficulty[i];
235+
f[d] = Math.max(f[d], profit[i]);
236+
}
237+
for (int i = 1; i <= m; ++i) {
238+
f[i] = Math.max(f[i], f[i - 1]);
239+
}
240+
int ans = 0;
241+
for (int w : worker) {
242+
ans += f[Math.min(w, m)];
243+
}
244+
return ans;
245+
}
246+
}
247+
```
248+
249+
#### C++
250+
251+
```cpp
252+
class Solution {
253+
public:
254+
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
255+
int m = *max_element(begin(difficulty), end(difficulty));
256+
int f[m + 1];
257+
memset(f, 0, sizeof(f));
258+
int n = profit.size();
259+
for (int i = 0; i < n; ++i) {
260+
int d = difficulty[i];
261+
f[d] = max(f[d], profit[i]);
262+
}
263+
for (int i = 1; i <= m; ++i) {
264+
f[i] = max(f[i], f[i - 1]);
265+
}
266+
int ans = 0;
267+
for (int w : worker) {
268+
ans += f[min(w, m)];
269+
}
270+
return ans;
271+
}
272+
};
273+
```
274+
275+
#### Go
276+
277+
```go
278+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
279+
m := slices.Max(difficulty)
280+
f := make([]int, m+1)
281+
for i, d := range difficulty {
282+
f[d] = max(f[d], profit[i])
283+
}
284+
for i := 1; i <= m; i++ {
285+
f[i] = max(f[i], f[i-1])
286+
}
287+
for _, w := range worker {
288+
ans += f[min(w, m)]
289+
}
290+
return
291+
}
292+
```
293+
294+
#### TypeScript
295+
296+
```ts
297+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
298+
const m = Math.max(...difficulty);
299+
const f = Array(m + 1).fill(0);
300+
const n = profit.length;
301+
for (let i = 0; i < n; ++i) {
302+
const d = difficulty[i];
303+
f[d] = Math.max(f[d], profit[i]);
304+
}
305+
for (let i = 1; i <= m; ++i) {
306+
f[i] = Math.max(f[i], f[i - 1]);
307+
}
308+
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
309+
}
310+
```
311+
312+
<!-- tabs:end -->
313+
314+
<!-- solution:end -->
315+
193316
<!-- problem:end -->

solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,127 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
190190

191191
<!-- solution:end -->
192192

193+
<!-- solution:start -->
194+
195+
### Solution 2: Dynamic Programming
196+
197+
Let's denote $m = \max(\text{difficulty})$ and define an array $f$ of length $m + 1$, where $f[i]$ represents the maximum profit among jobs with difficulty less than or equal to $i$, initially $f[i] = 0$.
198+
199+
Then, we iterate over the jobs, and for each job $(d, p)$, if $d \leq m$, we update $f[d] = \max(f[d], p)$.
200+
201+
Next, we iterate from $1$ to $m$, and for each $i$, we update $f[i] = \max(f[i], f[i - 1])$.
202+
203+
Finally, we iterate over the workers, and for each worker $w$, we add $f[w]$ to the answer.
204+
205+
The time complexity is $O(n + M)$, and the space complexity is $O(M)$. Here, $n$ is the length of the `profit` array, and $M$ is the maximum value in the `difficulty` array.
206+
207+
<!-- tabs:start -->
208+
209+
#### Python3
210+
211+
```python
212+
class Solution:
213+
def maxProfitAssignment(
214+
self, difficulty: List[int], profit: List[int], worker: List[int]
215+
) -> int:
216+
m = max(difficulty)
217+
f = [0] * (m + 1)
218+
for d, p in zip(difficulty, profit):
219+
f[d] = max(f[d], p)
220+
for i in range(1, m + 1):
221+
f[i] = max(f[i], f[i - 1])
222+
return sum(f[min(w, m)] for w in worker)
223+
```
224+
225+
#### Java
226+
227+
```java
228+
class Solution {
229+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
230+
int m = Arrays.stream(difficulty).max().getAsInt();
231+
int[] f = new int[m + 1];
232+
int n = profit.length;
233+
for (int i = 0; i < n; ++i) {
234+
int d = difficulty[i];
235+
f[d] = Math.max(f[d], profit[i]);
236+
}
237+
for (int i = 1; i <= m; ++i) {
238+
f[i] = Math.max(f[i], f[i - 1]);
239+
}
240+
int ans = 0;
241+
for (int w : worker) {
242+
ans += f[Math.min(w, m)];
243+
}
244+
return ans;
245+
}
246+
}
247+
```
248+
249+
#### C++
250+
251+
```cpp
252+
class Solution {
253+
public:
254+
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
255+
int m = *max_element(begin(difficulty), end(difficulty));
256+
int f[m + 1];
257+
memset(f, 0, sizeof(f));
258+
int n = profit.size();
259+
for (int i = 0; i < n; ++i) {
260+
int d = difficulty[i];
261+
f[d] = max(f[d], profit[i]);
262+
}
263+
for (int i = 1; i <= m; ++i) {
264+
f[i] = max(f[i], f[i - 1]);
265+
}
266+
int ans = 0;
267+
for (int w : worker) {
268+
ans += f[min(w, m)];
269+
}
270+
return ans;
271+
}
272+
};
273+
```
274+
275+
#### Go
276+
277+
```go
278+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
279+
m := slices.Max(difficulty)
280+
f := make([]int, m+1)
281+
for i, d := range difficulty {
282+
f[d] = max(f[d], profit[i])
283+
}
284+
for i := 1; i <= m; i++ {
285+
f[i] = max(f[i], f[i-1])
286+
}
287+
for _, w := range worker {
288+
ans += f[min(w, m)]
289+
}
290+
return
291+
}
292+
```
293+
294+
#### TypeScript
295+
296+
```ts
297+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
298+
const m = Math.max(...difficulty);
299+
const f = Array(m + 1).fill(0);
300+
const n = profit.length;
301+
for (let i = 0; i < n; ++i) {
302+
const d = difficulty[i];
303+
f[d] = Math.max(f[d], profit[i]);
304+
}
305+
for (let i = 1; i <= m; ++i) {
306+
f[i] = Math.max(f[i], f[i - 1]);
307+
}
308+
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
309+
}
310+
```
311+
312+
<!-- tabs:end -->
313+
314+
<!-- solution:end -->
315+
193316
<!-- problem:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
4+
int m = *max_element(begin(difficulty), end(difficulty));
5+
int f[m + 1];
6+
memset(f, 0, sizeof(f));
7+
int n = profit.size();
8+
for (int i = 0; i < n; ++i) {
9+
int d = difficulty[i];
10+
f[d] = max(f[d], profit[i]);
11+
}
12+
for (int i = 1; i <= m; ++i) {
13+
f[i] = max(f[i], f[i - 1]);
14+
}
15+
int ans = 0;
16+
for (int w : worker) {
17+
ans += f[min(w, m)];
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
2+
m := slices.Max(difficulty)
3+
f := make([]int, m+1)
4+
for i, d := range difficulty {
5+
f[d] = max(f[d], profit[i])
6+
}
7+
for i := 1; i <= m; i++ {
8+
f[i] = max(f[i], f[i-1])
9+
}
10+
for _, w := range worker {
11+
ans += f[min(w, m)]
12+
}
13+
return
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
3+
int m = Arrays.stream(difficulty).max().getAsInt();
4+
int[] f = new int[m + 1];
5+
int n = profit.length;
6+
for (int i = 0; i < n; ++i) {
7+
int d = difficulty[i];
8+
f[d] = Math.max(f[d], profit[i]);
9+
}
10+
for (int i = 1; i <= m; ++i) {
11+
f[i] = Math.max(f[i], f[i - 1]);
12+
}
13+
int ans = 0;
14+
for (int w : worker) {
15+
ans += f[Math.min(w, m)];
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maxProfitAssignment(
3+
self, difficulty: List[int], profit: List[int], worker: List[int]
4+
) -> int:
5+
m = max(difficulty)
6+
f = [0] * (m + 1)
7+
for d, p in zip(difficulty, profit):
8+
f[d] = max(f[d], p)
9+
for i in range(1, m + 1):
10+
f[i] = max(f[i], f[i - 1])
11+
return sum(f[min(w, m)] for w in worker)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
2+
const m = Math.max(...difficulty);
3+
const f = Array(m + 1).fill(0);
4+
const n = profit.length;
5+
for (let i = 0; i < n; ++i) {
6+
const d = difficulty[i];
7+
f[d] = Math.max(f[d], profit[i]);
8+
}
9+
for (let i = 1; i <= m; ++i) {
10+
f[i] = Math.max(f[i], f[i - 1]);
11+
}
12+
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
13+
}

0 commit comments

Comments
 (0)