forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0826 (doocs#2826)
No.0826.Most Profit Assigning Work
- Loading branch information
Showing
7 changed files
with
179 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 11 additions & 12 deletions
23
solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
solution/0800-0899/0826.Most Profit Assigning Work/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.