Skip to content

feat: add solutions to lc problem: No.0826 #3123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,127 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num

<!-- solution:end -->

<!-- solution:start -->

### 方法二:动态规划

我们不妨记 $m = \max(\text{difficulty})$,定义一个长度为 $m + 1$ 的数组 $f$,其中 $f[i]$ 表示难度小于等于 $i$ 的工作中收益的最大值,初始时 $f[i] = 0$。

然后我们遍历工作,对于每个工作 $(d, p)$,我们更新 $f[d] = \max(f[d], p)$。

接下来,我们从 $1$ 到 $m$ 遍历,对于每个 $i$,我们更新 $f[i] = \max(f[i], f[i - 1])$。

最后,我们遍历工人,对于每个工人 $w$,我们将 $f[\min(w, m)]$ 加到答案中。

时间复杂度 $O(n + M)$,空间复杂度 $O(M)$。其中 $n$ 是数组 `profit` 的长度,而 $M$ 是数组 `difficulty` 中的最大值。

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxProfitAssignment(
self, difficulty: List[int], profit: List[int], worker: List[int]
) -> int:
m = max(difficulty)
f = [0] * (m + 1)
for d, p in zip(difficulty, profit):
f[d] = max(f[d], p)
for i in range(1, m + 1):
f[i] = max(f[i], f[i - 1])
return sum(f[min(w, m)] for w in worker)
```

#### Java

```java
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int m = Arrays.stream(difficulty).max().getAsInt();
int[] f = new int[m + 1];
int n = profit.length;
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[Math.min(w, m)];
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int m = *max_element(begin(difficulty), end(difficulty));
int f[m + 1];
memset(f, 0, sizeof(f));
int n = profit.size();
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[min(w, m)];
}
return ans;
}
};
```

#### Go

```go
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
m := slices.Max(difficulty)
f := make([]int, m+1)
for i, d := range difficulty {
f[d] = max(f[d], profit[i])
}
for i := 1; i <= m; i++ {
f[i] = max(f[i], f[i-1])
}
for _, w := range worker {
ans += f[min(w, m)]
}
return
}
```

#### TypeScript

```ts
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
const m = Math.max(...difficulty);
const f = Array(m + 1).fill(0);
const n = profit.length;
for (let i = 0; i < n; ++i) {
const d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (let i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
123 changes: 123 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,127 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num

<!-- solution:end -->

<!-- solution:start -->

### Solution 2: Dynamic Programming

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$.

Then, we iterate over the jobs, and for each job $(d, p)$, if $d \leq m$, we update $f[d] = \max(f[d], p)$.

Next, we iterate from $1$ to $m$, and for each $i$, we update $f[i] = \max(f[i], f[i - 1])$.

Finally, we iterate over the workers, and for each worker $w$, we add $f[w]$ to the answer.

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.

<!-- tabs:start -->

#### Python3

```python
class Solution:
def maxProfitAssignment(
self, difficulty: List[int], profit: List[int], worker: List[int]
) -> int:
m = max(difficulty)
f = [0] * (m + 1)
for d, p in zip(difficulty, profit):
f[d] = max(f[d], p)
for i in range(1, m + 1):
f[i] = max(f[i], f[i - 1])
return sum(f[min(w, m)] for w in worker)
```

#### Java

```java
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int m = Arrays.stream(difficulty).max().getAsInt();
int[] f = new int[m + 1];
int n = profit.length;
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[Math.min(w, m)];
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int m = *max_element(begin(difficulty), end(difficulty));
int f[m + 1];
memset(f, 0, sizeof(f));
int n = profit.size();
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[min(w, m)];
}
return ans;
}
};
```

#### Go

```go
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
m := slices.Max(difficulty)
f := make([]int, m+1)
for i, d := range difficulty {
f[d] = max(f[d], profit[i])
}
for i := 1; i <= m; i++ {
f[i] = max(f[i], f[i-1])
}
for _, w := range worker {
ans += f[min(w, m)]
}
return
}
```

#### TypeScript

```ts
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
const m = Math.max(...difficulty);
const f = Array(m + 1).fill(0);
const n = profit.length;
for (let i = 0; i < n; ++i) {
const d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (let i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
21 changes: 21 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int m = *max_element(begin(difficulty), end(difficulty));
int f[m + 1];
memset(f, 0, sizeof(f));
int n = profit.size();
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[min(w, m)];
}
return ans;
}
};
14 changes: 14 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) {
m := slices.Max(difficulty)
f := make([]int, m+1)
for i, d := range difficulty {
f[d] = max(f[d], profit[i])
}
for i := 1; i <= m; i++ {
f[i] = max(f[i], f[i-1])
}
for _, w := range worker {
ans += f[min(w, m)]
}
return
}
19 changes: 19 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
int m = Arrays.stream(difficulty).max().getAsInt();
int[] f = new int[m + 1];
int n = profit.length;
for (int i = 0; i < n; ++i) {
int d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (int i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
int ans = 0;
for (int w : worker) {
ans += f[Math.min(w, m)];
}
return ans;
}
}
11 changes: 11 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def maxProfitAssignment(
self, difficulty: List[int], profit: List[int], worker: List[int]
) -> int:
m = max(difficulty)
f = [0] * (m + 1)
for d, p in zip(difficulty, profit):
f[d] = max(f[d], p)
for i in range(1, m + 1):
f[i] = max(f[i], f[i - 1])
return sum(f[min(w, m)] for w in worker)
13 changes: 13 additions & 0 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
const m = Math.max(...difficulty);
const f = Array(m + 1).fill(0);
const n = profit.length;
for (let i = 0; i < n; ++i) {
const d = difficulty[i];
f[d] = Math.max(f[d], profit[i]);
}
for (let i = 1; i <= m; ++i) {
f[i] = Math.max(f[i], f[i - 1]);
}
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
}
Loading