From 7a7d91f5d03c87d8d6f74f309875703e22c8b35a Mon Sep 17 00:00:00 2001 From: rain84 Date: Tue, 18 Jun 2024 19:04:25 +0300 Subject: [PATCH 1/9] feat: add ts solution to lc problem: No.0826 --- .../0826.Most Profit Assigning Work/README.md | 31 +++++++++++++++++++ .../README_EN.md | 31 +++++++++++++++++++ .../Solution2.ts | 16 ++++++++++ 3 files changed, 78 insertions(+) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README.md b/solution/0800-0899/0826.Most Profit Assigning Work/README.md index ac375926f274d..86e92d250c100 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README.md @@ -190,4 +190,35 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num + + +### Solution 2: Memoization + + + +#### TypeScript + +```ts +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const wMax = Math.max(...worker); + const jobs = Array(wMax + 1).fill(0); + const n = profit.length; + + for (let i = 0; i < n; i++) { + const d = difficulty[i]; + if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + } + + for (let i = 1, x = 0; i <= wMax; i++) { + jobs[i] = Math.max(jobs[i], jobs[i - 1]); + } + + return worker.reduce((acc, w) => acc + jobs[w], 0); +} +``` + + + + + diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md index 6f2f13975e767..5fbd43fd6078c 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md @@ -190,4 +190,35 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num + + +### Solution 2: Memoization + + + +#### TypeScript + +```ts +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const wMax = Math.max(...worker); + const jobs = Array(wMax + 1).fill(0); + const n = profit.length; + + for (let i = 0; i < n; i++) { + const d = difficulty[i]; + if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + } + + for (let i = 1, x = 0; i <= wMax; i++) { + jobs[i] = Math.max(jobs[i], jobs[i - 1]); + } + + return worker.reduce((acc, w) => acc + jobs[w], 0); +} +``` + + + + + diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts new file mode 100644 index 0000000000000..e72cb28add2d4 --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts @@ -0,0 +1,16 @@ +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const wMax = Math.max(...worker); + const jobs = Array(wMax + 1).fill(0); + const n = profit.length; + + for (let i = 0; i < n; i++) { + const d = difficulty[i]; + if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + } + + for (let i = 1, x = 0; i <= wMax; i++) { + jobs[i] = Math.max(jobs[i], jobs[i - 1]); + } + + return worker.reduce((acc, w) => acc + jobs[w], 0); +} From 7d4b1f37bab42f7bb7cda4e3c836e45d6c8650a3 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:32:23 +0800 Subject: [PATCH 2/9] Update README.md --- .../0826.Most Profit Assigning Work/README.md | 115 ++++++++++++++++-- 1 file changed, 104 insertions(+), 11 deletions(-) diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README.md b/solution/0800-0899/0826.Most Profit Assigning Work/README.md index 86e92d250c100..575e91d26a506 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README.md @@ -192,28 +192,120 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num -### Solution 2: Memoization +### 方法二:动态规划 + +我们不妨记 $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` 中的最大值。 +#### 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& difficulty, vector& profit, vector& 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 wMax = Math.max(...worker); - const jobs = Array(wMax + 1).fill(0); + const m = Math.max(...difficulty); + const f = Array(m + 1).fill(0); const n = profit.length; - - for (let i = 0; i < n; i++) { + for (let i = 0; i < n; ++i) { const d = difficulty[i]; - if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + f[d] = Math.max(f[d], profit[i]); } - - for (let i = 1, x = 0; i <= wMax; i++) { - jobs[i] = Math.max(jobs[i], jobs[i - 1]); + for (let i = 1; i <= m; ++i) { + f[i] = Math.max(f[i], f[i - 1]); } - - return worker.reduce((acc, w) => acc + jobs[w], 0); + return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0); } ``` @@ -222,3 +314,4 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num + From 8b27d05af90bc2bcc66eb44405bfaa9bef300a08 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:32:52 +0800 Subject: [PATCH 3/9] Update README_EN.md --- .../README_EN.md | 115 ++++++++++++++++-- 1 file changed, 104 insertions(+), 11 deletions(-) diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md index 5fbd43fd6078c..b063e59e0465b 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md @@ -192,28 +192,120 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num -### Solution 2: Memoization +### 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. +#### 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& difficulty, vector& profit, vector& 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 wMax = Math.max(...worker); - const jobs = Array(wMax + 1).fill(0); + const m = Math.max(...difficulty); + const f = Array(m + 1).fill(0); const n = profit.length; - - for (let i = 0; i < n; i++) { + for (let i = 0; i < n; ++i) { const d = difficulty[i]; - if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + f[d] = Math.max(f[d], profit[i]); } - - for (let i = 1, x = 0; i <= wMax; i++) { - jobs[i] = Math.max(jobs[i], jobs[i - 1]); + for (let i = 1; i <= m; ++i) { + f[i] = Math.max(f[i], f[i - 1]); } - - return worker.reduce((acc, w) => acc + jobs[w], 0); + return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0); } ``` @@ -222,3 +314,4 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num + From a12a4e7982126bf692f7a217ed6cb6aaf639b0e2 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:33:02 +0800 Subject: [PATCH 4/9] Update Solution2.ts --- .../Solution2.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts index e72cb28add2d4..2967c42fd7a26 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.ts @@ -1,16 +1,13 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { - const wMax = Math.max(...worker); - const jobs = Array(wMax + 1).fill(0); + const m = Math.max(...difficulty); + const f = Array(m + 1).fill(0); const n = profit.length; - - for (let i = 0; i < n; i++) { + for (let i = 0; i < n; ++i) { const d = difficulty[i]; - if (d <= wMax) jobs[d] = Math.max(jobs[d], profit[i]); + f[d] = Math.max(f[d], profit[i]); } - - for (let i = 1, x = 0; i <= wMax; i++) { - jobs[i] = Math.max(jobs[i], jobs[i - 1]); + for (let i = 1; i <= m; ++i) { + f[i] = Math.max(f[i], f[i - 1]); } - - return worker.reduce((acc, w) => acc + jobs[w], 0); + return worker.reduce((acc, w) => acc + f[Math.min(w, m)], 0); } From 789b58b22da47980166d422b2e949f346dd7be16 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:33:32 +0800 Subject: [PATCH 5/9] Create Solution2.py --- .../0826.Most Profit Assigning Work/Solution2.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution2.py diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.py b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.py new file mode 100644 index 0000000000000..7d5e992786776 --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.py @@ -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) From 7258c1f5b9dffffce11f4e7f500cc97b69937a89 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:33:41 +0800 Subject: [PATCH 6/9] Create Solution2.java --- .../Solution2.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution2.java diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.java b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.java new file mode 100644 index 0000000000000..134c322057cad --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.java @@ -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; + } +} From ddf783cef329ef8507dae7f9e825840b196b1d8f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:33:53 +0800 Subject: [PATCH 7/9] Create Solution2.cpp --- .../Solution2.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution2.cpp diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.cpp b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.cpp new file mode 100644 index 0000000000000..84114264adcf1 --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int maxProfitAssignment(vector& difficulty, vector& profit, vector& 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; + } +}; From 33b6c1cfbbaf99752cdb7001e264bcaef655f329 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 19 Jun 2024 11:34:04 +0800 Subject: [PATCH 8/9] Create Solution2.go --- .../0826.Most Profit Assigning Work/Solution2.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution2.go diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.go b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.go new file mode 100644 index 0000000000000..97a45eef59f55 --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution2.go @@ -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 +} From be4a24377c80f6f5ae4007a5a1c2c2c6cffb63e4 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 19 Jun 2024 03:34:58 +0000 Subject: [PATCH 9/9] style: format code and docs with prettier --- solution/0800-0899/0826.Most Profit Assigning Work/README.md | 1 - solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md | 1 - 2 files changed, 2 deletions(-) diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README.md b/solution/0800-0899/0826.Most Profit Assigning Work/README.md index 575e91d26a506..468f898113a8b 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README.md @@ -314,4 +314,3 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num - diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md index b063e59e0465b..d9edd25f594fc 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md @@ -314,4 +314,3 @@ function maxProfitAssignment(difficulty: number[], profit: number[], worker: num -