From 036dff436ed53cab6b1091e729a217c5f6ad2804 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 17 May 2024 10:05:07 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.0826 No.0826.Most Profit Assigning Work --- .../0826.Most Profit Assigning Work/README.md | 108 ++++++++++-------- .../README_EN.md | 108 ++++++++++-------- .../Solution.cpp | 23 ++-- .../Solution.go | 25 ++-- .../Solution.java | 21 ++-- .../Solution.py | 14 +-- .../Solution.ts | 14 +++ 7 files changed, 179 insertions(+), 134 deletions(-) create mode 100644 solution/0800-0899/0826.Most Profit Assigning Work/Solution.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 852fe07924159..32e27f1a154d2 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/README.md +++ b/solution/0800-0899/0826.Most Profit Assigning Work/README.md @@ -68,7 +68,13 @@ tags: -### 方法一 +### 方法一:排序 + 双指针 + +我们可以将工作按照能力升序排列,然后将工作按照难度升序排列。 + +然后我们遍历工人,对于每个工人,我们找出他能完成的工作中收益最大的那个,然后将这个收益加到答案中。 + +时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 `profit` 和 `worker` 的长度。 @@ -77,38 +83,35 @@ class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: - n = len(difficulty) - job = [(difficulty[i], profit[i]) for i in range(n)] - job.sort(key=lambda x: x[0]) worker.sort() - i = t = res = 0 + jobs = sorted(zip(difficulty, profit)) + ans = mx = i = 0 for w in worker: - while i < n and job[i][0] <= w: - t = max(t, job[i][1]) + while i < len(jobs) and jobs[i][0] <= w: + mx = max(mx, jobs[i][1]) i += 1 - res += t - return res + ans += mx + return ans ``` ```java class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { - int n = difficulty.length; - List job = new ArrayList<>(); + Arrays.sort(worker); + int n = profit.length; + int[][] jobs = new int[n][0]; for (int i = 0; i < n; ++i) { - job.add(new int[] {difficulty[i], profit[i]}); + jobs[i] = new int[] {difficulty[i], profit[i]}; } - job.sort(Comparator.comparing(a -> a[0])); - Arrays.sort(worker); - int res = 0; - int i = 0, t = 0; + Arrays.sort(jobs, (a, b) -> a[0] - b[0]); + int ans = 0, mx = 0, i = 0; for (int w : worker) { - while (i < n && job.get(i)[0] <= w) { - t = Math.max(t, job.get(i++)[1]); + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); } - res += t; + ans += mx; } - return res; + return ans; } } ``` @@ -117,44 +120,59 @@ class Solution { class Solution { public: int maxProfitAssignment(vector& difficulty, vector& profit, vector& worker) { - int n = difficulty.size(); - vector> job; + sort(worker.begin(), worker.end()); + int n = profit.size(); + vector> jobs; for (int i = 0; i < n; ++i) { - job.push_back({difficulty[i], profit[i]}); + jobs.emplace_back(difficulty[i], profit[i]); } - sort(job.begin(), job.end()); - sort(worker.begin(), worker.end()); - int i = 0, t = 0; - int res = 0; - for (auto w : worker) { - while (i < n && job[i].first <= w) { - t = max(t, job[i++].second); + sort(jobs.begin(), jobs.end()); + int ans = 0, mx = 0, i = 0; + for (int w : worker) { + while (i < n && jobs[i].first <= w) { + mx = max(mx, jobs[i++].second); } - res += t; + ans += mx; } - return res; + return ans; } }; ``` ```go -func maxProfitAssignment(difficulty []int, profit []int, worker []int) int { - var job [][2]int - for i := range difficulty { - job = append(job, [2]int{difficulty[i], profit[i]}) - } - - sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] }) +func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) { sort.Ints(worker) - i, t, n, res := 0, 0, len(difficulty), 0 + n := len(profit) + jobs := make([][2]int, n) + for i, p := range profit { + jobs[i] = [2]int{difficulty[i], p} + } + sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] }) + mx, i := 0, 0 for _, w := range worker { - for i < n && job[i][0] <= w { - t = max(t, job[i][1]) - i++ + for ; i < n && jobs[i][0] <= w; i++ { + mx = max(mx, jobs[i][1]) } - res += t + ans += mx } - return res + return +} +``` + +```ts +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const n = profit.length; + worker.sort((a, b) => a - b); + const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]); + jobs.sort((a, b) => a[0] - b[0]); + let [ans, mx, i] = [0, 0, 0]; + for (const w of worker) { + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); + } + ans += mx; + } + return ans; } ``` 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 d1b03056ff504..a26f965bd4c11 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 @@ -68,7 +68,13 @@ tags: -### Solution 1 +### Solution 1: Sorting + Two Pointers + +We can sort the jobs in ascending order of ability, and then sort the jobs in ascending order of difficulty. + +Then we traverse the workers. For each worker, we find the job with the maximum profit that he can complete, and then add this profit to the answer. + +The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(n)$. Where $n$ and $m$ are the lengths of the arrays `profit` and `worker` respectively. @@ -77,38 +83,35 @@ class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: - n = len(difficulty) - job = [(difficulty[i], profit[i]) for i in range(n)] - job.sort(key=lambda x: x[0]) worker.sort() - i = t = res = 0 + jobs = sorted(zip(difficulty, profit)) + ans = mx = i = 0 for w in worker: - while i < n and job[i][0] <= w: - t = max(t, job[i][1]) + while i < len(jobs) and jobs[i][0] <= w: + mx = max(mx, jobs[i][1]) i += 1 - res += t - return res + ans += mx + return ans ``` ```java class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { - int n = difficulty.length; - List job = new ArrayList<>(); + Arrays.sort(worker); + int n = profit.length; + int[][] jobs = new int[n][0]; for (int i = 0; i < n; ++i) { - job.add(new int[] {difficulty[i], profit[i]}); + jobs[i] = new int[] {difficulty[i], profit[i]}; } - job.sort(Comparator.comparing(a -> a[0])); - Arrays.sort(worker); - int res = 0; - int i = 0, t = 0; + Arrays.sort(jobs, (a, b) -> a[0] - b[0]); + int ans = 0, mx = 0, i = 0; for (int w : worker) { - while (i < n && job.get(i)[0] <= w) { - t = Math.max(t, job.get(i++)[1]); + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); } - res += t; + ans += mx; } - return res; + return ans; } } ``` @@ -117,44 +120,59 @@ class Solution { class Solution { public: int maxProfitAssignment(vector& difficulty, vector& profit, vector& worker) { - int n = difficulty.size(); - vector> job; + sort(worker.begin(), worker.end()); + int n = profit.size(); + vector> jobs; for (int i = 0; i < n; ++i) { - job.push_back({difficulty[i], profit[i]}); + jobs.emplace_back(difficulty[i], profit[i]); } - sort(job.begin(), job.end()); - sort(worker.begin(), worker.end()); - int i = 0, t = 0; - int res = 0; - for (auto w : worker) { - while (i < n && job[i].first <= w) { - t = max(t, job[i++].second); + sort(jobs.begin(), jobs.end()); + int ans = 0, mx = 0, i = 0; + for (int w : worker) { + while (i < n && jobs[i].first <= w) { + mx = max(mx, jobs[i++].second); } - res += t; + ans += mx; } - return res; + return ans; } }; ``` ```go -func maxProfitAssignment(difficulty []int, profit []int, worker []int) int { - var job [][2]int - for i := range difficulty { - job = append(job, [2]int{difficulty[i], profit[i]}) - } - - sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] }) +func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) { sort.Ints(worker) - i, t, n, res := 0, 0, len(difficulty), 0 + n := len(profit) + jobs := make([][2]int, n) + for i, p := range profit { + jobs[i] = [2]int{difficulty[i], p} + } + sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] }) + mx, i := 0, 0 for _, w := range worker { - for i < n && job[i][0] <= w { - t = max(t, job[i][1]) - i++ + for ; i < n && jobs[i][0] <= w; i++ { + mx = max(mx, jobs[i][1]) } - res += t + ans += mx } - return res + return +} +``` + +```ts +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const n = profit.length; + worker.sort((a, b) => a - b); + const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]); + jobs.sort((a, b) => a[0] - b[0]); + let [ans, mx, i] = [0, 0, 0]; + for (const w of worker) { + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); + } + ans += mx; + } + return ans; } ``` diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp index 53a38cb8ee18a..014925198fabd 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp @@ -1,21 +1,20 @@ class Solution { public: int maxProfitAssignment(vector& difficulty, vector& profit, vector& worker) { - int n = difficulty.size(); - vector> job; + sort(worker.begin(), worker.end()); + int n = profit.size(); + vector> jobs; for (int i = 0; i < n; ++i) { - job.push_back({difficulty[i], profit[i]}); + jobs.emplace_back(difficulty[i], profit[i]); } - sort(job.begin(), job.end()); - sort(worker.begin(), worker.end()); - int i = 0, t = 0; - int res = 0; - for (auto w : worker) { - while (i < n && job[i].first <= w) { - t = max(t, job[i++].second); + sort(jobs.begin(), jobs.end()); + int ans = 0, mx = 0, i = 0; + for (int w : worker) { + while (i < n && jobs[i].first <= w) { + mx = max(mx, jobs[i++].second); } - res += t; + ans += mx; } - return res; + return ans; } }; \ No newline at end of file diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.go b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.go index 1d4400ac8965c..1c04d8abe3b40 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.go +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.go @@ -1,18 +1,17 @@ -func maxProfitAssignment(difficulty []int, profit []int, worker []int) int { - var job [][2]int - for i := range difficulty { - job = append(job, [2]int{difficulty[i], profit[i]}) - } - - sort.SliceStable(job, func(i, j int) bool { return job[i][0] <= job[j][0] }) +func maxProfitAssignment(difficulty []int, profit []int, worker []int) (ans int) { sort.Ints(worker) - i, t, n, res := 0, 0, len(difficulty), 0 + n := len(profit) + jobs := make([][2]int, n) + for i, p := range profit { + jobs[i] = [2]int{difficulty[i], p} + } + sort.Slice(jobs, func(i, j int) bool { return jobs[i][0] < jobs[j][0] }) + mx, i := 0, 0 for _, w := range worker { - for i < n && job[i][0] <= w { - t = max(t, job[i][1]) - i++ + for ; i < n && jobs[i][0] <= w; i++ { + mx = max(mx, jobs[i][1]) } - res += t + ans += mx } - return res + return } \ No newline at end of file diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.java b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.java index 6826ef4645ef7..8dd3908e678c7 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.java +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.java @@ -1,20 +1,19 @@ class Solution { public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) { - int n = difficulty.length; - List job = new ArrayList<>(); + Arrays.sort(worker); + int n = profit.length; + int[][] jobs = new int[n][0]; for (int i = 0; i < n; ++i) { - job.add(new int[] {difficulty[i], profit[i]}); + jobs[i] = new int[] {difficulty[i], profit[i]}; } - job.sort(Comparator.comparing(a -> a[0])); - Arrays.sort(worker); - int res = 0; - int i = 0, t = 0; + Arrays.sort(jobs, (a, b) -> a[0] - b[0]); + int ans = 0, mx = 0, i = 0; for (int w : worker) { - while (i < n && job.get(i)[0] <= w) { - t = Math.max(t, job.get(i++)[1]); + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); } - res += t; + ans += mx; } - return res; + return ans; } } \ No newline at end of file diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.py b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.py index 1ea7bd50ba9cc..d3852eae64fcf 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.py +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.py @@ -2,14 +2,12 @@ class Solution: def maxProfitAssignment( self, difficulty: List[int], profit: List[int], worker: List[int] ) -> int: - n = len(difficulty) - job = [(difficulty[i], profit[i]) for i in range(n)] - job.sort(key=lambda x: x[0]) worker.sort() - i = t = res = 0 + jobs = sorted(zip(difficulty, profit)) + ans = mx = i = 0 for w in worker: - while i < n and job[i][0] <= w: - t = max(t, job[i][1]) + while i < len(jobs) and jobs[i][0] <= w: + mx = max(mx, jobs[i][1]) i += 1 - res += t - return res + ans += mx + return ans diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.ts b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.ts new file mode 100644 index 0000000000000..6102fc1b28c8d --- /dev/null +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.ts @@ -0,0 +1,14 @@ +function maxProfitAssignment(difficulty: number[], profit: number[], worker: number[]): number { + const n = profit.length; + worker.sort((a, b) => a - b); + const jobs = Array.from({ length: n }, (_, i) => [difficulty[i], profit[i]]); + jobs.sort((a, b) => a[0] - b[0]); + let [ans, mx, i] = [0, 0, 0]; + for (const w of worker) { + while (i < n && jobs[i][0] <= w) { + mx = Math.max(mx, jobs[i++][1]); + } + ans += mx; + } + return ans; +}