Skip to content

Commit 8b27d05

Browse files
authored
Update README_EN.md
1 parent 7d4b1f3 commit 8b27d05

File tree

1 file changed

+104
-11
lines changed
  • solution/0800-0899/0826.Most Profit Assigning Work

1 file changed

+104
-11
lines changed

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

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,28 +192,120 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
192192

193193
<!-- solution:start -->
194194

195-
### Solution 2: Memoization
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.
196206

197207
<!-- tabs:start -->
198208

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+
199294
#### TypeScript
200295

201296
```ts
202297
function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number {
203-
const wMax = Math.max(...worker);
204-
const jobs = Array(wMax + 1).fill(0);
298+
const m = Math.max(...difficulty);
299+
const f = Array(m + 1).fill(0);
205300
const n = profit.length;
206-
207-
for (let i = 0; i < n; i++) {
301+
for (let i = 0; i < n; ++i) {
208302
const d = difficulty[i];
209-
if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]);
303+
f[d] = Math.max(f[d], profit[i]);
210304
}
211-
212-
for (let i = 1, x = 0; i <= wMax; i++) {
213-
jobs[i] = Math.max(jobs[i], jobs[i - 1]);
305+
for (let i = 1; i <= m; ++i) {
306+
f[i] = Math.max(f[i], f[i - 1]);
214307
}
215-
216-
return worker.reduce((acc, w) => acc + jobs[w], 0);
308+
return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0);
217309
}
218310
```
219311

@@ -222,3 +314,4 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num
222314
<!-- solution:end -->
223315

224316
<!-- problem:end -->
317+

0 commit comments

Comments
 (0)