Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b4ec0a

Browse files
Merge pull request #46 from codewithhridoy/javascript
medium: 826. Most Profit Assigning Work
2 parents f82e64e + 86d6d34 commit 3b4ec0a

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {number[]} difficulty
3+
* @param {number[]} profit
4+
* @param {number[]} worker
5+
* @return {number}
6+
*/
7+
function maxProfitAssignment(difficulty, profit, worker) {
8+
// Pair each job's difficulty with its profit and sort by difficulty
9+
let jobs = difficulty.map((d, i) => [d, profit[i]]);
10+
jobs.sort((a, b) => a[0] - b[0]);
11+
12+
// Sort workers based on their abilities
13+
worker.sort((a, b) => a - b);
14+
15+
let maxProfit = 0;
16+
let totalProfit = 0;
17+
let i = 0;
18+
let n = jobs.length;
19+
20+
// Iterate through each worker
21+
for (let ability of worker) {
22+
// Update maxProfit for the current worker's ability
23+
while (i < n && jobs[i][0] <= ability) {
24+
maxProfit = Math.max(maxProfit, jobs[i][1]);
25+
i++;
26+
}
27+
// Add the best profit the current worker can get
28+
totalProfit += maxProfit;
29+
}
30+
31+
return totalProfit;
32+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# [826. Most Profit Assigning Work](https://leetcode.com/problems/most-profit-assigning-work)
2+
3+
---
4+
5+
title: "Max Profit Assignment"
6+
summary: "A solution to the problem of maximizing profit by assigning workers to jobs based on their abilities."
7+
date: "2024年06月18日"
8+
modified_date: "2024年06月18日"
9+
tags: ["Algorithm", "JavaScript", "Dynamic Programming"]
10+
slug: "max-profit-assignment"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/a6701003-76f7-4bbf-9d2f-f844e663913a_1718675534.1854534.png)
15+
16+
# Intuition
17+
18+
To solve this problem, the first thought is to match each worker's ability with the most profitable job they can do. This requires sorting both the jobs by their difficulty and the workers by their ability. By iterating through the workers and keeping track of the best profit they can achieve based on their ability, we can ensure that each worker is assigned the most profitable job they can handle.
19+
20+
# Approach
21+
22+
The approach involves the following steps:
23+
24+
1. Pair each job's difficulty with its profit and sort the jobs by difficulty.
25+
2. Sort the workers based on their abilities.
26+
3. Iterate through each worker and for each worker, update the maximum profit they can achieve based on the jobs they can handle.
27+
4. Sum up the profits for all workers to get the total profit.
28+
29+
# Complexity
30+
31+
- Time complexity: $$O(n \log n + m \log m)$$, where $$n$$ is the number of jobs and $$m$$ is the number of workers. This accounts for the sorting of the jobs and workers.
32+
33+
- Space complexity: $$O(n)$$ for storing the job pairs.
34+
35+
# Code
36+
37+
```javascript
38+
/**
39+
* @param {number[]} difficulty
40+
* @param {number[]} profit
41+
* @param {number[]} worker
42+
* @return {number}
43+
*/
44+
function maxProfitAssignment(difficulty, profit, worker) {
45+
// Pair each job's difficulty with its profit and sort by difficulty
46+
let jobs = difficulty.map((d, i) => [d, profit[i]]);
47+
jobs.sort((a, b) => a[0] - b[0]);
48+
49+
// Sort workers based on their abilities
50+
worker.sort((a, b) => a - b);
51+
52+
let maxProfit = 0;
53+
let totalProfit = 0;
54+
let i = 0;
55+
let n = jobs.length;
56+
57+
// Iterate through each worker
58+
for (let ability of worker) {
59+
// Update maxProfit for the current worker's ability
60+
while (i < n && jobs[i][0] <= ability) {
61+
maxProfit = Math.max(maxProfit, jobs[i][1]);
62+
i++;
63+
}
64+
// Add the best profit the current worker can get
65+
totalProfit += maxProfit;
66+
}
67+
68+
return totalProfit;
69+
}
70+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /