Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0826 (doocs#2826)
Browse files Browse the repository at this point in the history
No.0826.Most Profit Assigning Work
  • Loading branch information
yanglbme authored May 17, 2024
1 parent cc937dc commit 9ffe05e
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 134 deletions.
108 changes: 63 additions & 45 deletions solution/0800-0899/0826.Most Profit Assigning Work/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:排序 + 双指针

我们可以将工作按照能力升序排列,然后将工作按照难度升序排列。

然后我们遍历工人,对于每个工人,我们找出他能完成的工作中收益最大的那个,然后将这个收益加到答案中。

时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 `profit``worker` 的长度。

<!-- tabs:start -->

Expand All @@ -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<int[]> 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;
}
}
```
Expand All @@ -117,44 +120,59 @@ class Solution {
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int n = difficulty.size();
vector<pair<int, int>> job;
sort(worker.begin(), worker.end());
int n = profit.size();
vector<pair<int, int>> 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;
}
```

Expand Down
108 changes: 63 additions & 45 deletions solution/0800-0899/0826.Most Profit Assigning Work/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ tags:

<!-- solution:start -->

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

<!-- tabs:start -->

Expand All @@ -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<int[]> 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;
}
}
```
Expand All @@ -117,44 +120,59 @@ class Solution {
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int n = difficulty.size();
vector<pair<int, int>> job;
sort(worker.begin(), worker.end());
int n = profit.size();
vector<pair<int, int>> 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;
}
```

Expand Down
23 changes: 11 additions & 12 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
class Solution {
public:
int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
int n = difficulty.size();
vector<pair<int, int>> job;
sort(worker.begin(), worker.end());
int n = profit.size();
vector<pair<int, int>> 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;
}
};
25 changes: 12 additions & 13 deletions solution/0800-0899/0826.Most Profit Assigning Work/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading

0 comments on commit 9ffe05e

Please sign in to comment.