-
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.
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
## 826. Most Profit Assigning Work - JavaScript Solution | ||
|
||
### Problem Statement | ||
|
||
You have `n` jobs, where every job is represented by its difficulty and profit. Additionally, you have `m` workers, where each worker can only be assigned to a job if the job's difficulty level is less than or equal to the worker's ability. Every worker can be assigned to at most one job, and each job can be assigned to at most one worker. Your goal is to maximize the total profit by optimally assigning jobs to workers. | ||
|
||
### Example | ||
|
||
**Input:** | ||
- `difficulty = [2, 4, 6, 8, 10]` | ||
- `profit = [10, 20, 30, 40, 50]` | ||
- `worker = [4, 5, 6, 7]` | ||
|
||
**Output:** | ||
- `100` | ||
|
||
### Approach | ||
|
||
To solve this problem, we'll take the following steps: | ||
1. Combine the `difficulty` and `profit` arrays into a list of jobs. | ||
2. Sort the jobs based on difficulty. | ||
3. Sort the workers based on their ability. | ||
4. Use a two-pointer technique to assign the most profitable job to each worker based on their ability. | ||
|
||
### Solution | ||
|
||
Here’s the JavaScript solution to the problem: | ||
|
||
```javascript | ||
function maxProfitAssignment(difficulty, profit, worker) { | ||
// Step 1: Combine difficulty and profit into jobs array | ||
let jobs = []; | ||
for (let i = 0; i < difficulty.length; i++) { | ||
jobs.push([difficulty[i], profit[i]]); | ||
} | ||
|
||
// Step 2: Sort jobs by difficulty | ||
jobs.sort((a, b) => a[0] - b[0]); | ||
|
||
// Step 3: Sort workers by their abilities | ||
worker.sort((a, b) => a - b); | ||
|
||
let maxProfit = 0; | ||
let jobIndex = 0; | ||
let bestProfit = 0; | ||
|
||
// Step 4: Iterate through each worker | ||
for (let i = 0; i < worker.length; i++) { | ||
let workerAbility = worker[i]; | ||
|
||
// Find the best profit for the current worker's ability | ||
while (jobIndex < jobs.length && jobs[jobIndex][0] <= workerAbility) { | ||
bestProfit = Math.max(bestProfit, jobs[jobIndex][1]); | ||
jobIndex++; | ||
} | ||
|
||
maxProfit += bestProfit; | ||
} | ||
|
||
return maxProfit; | ||
} | ||
|
||
// Example usage: | ||
const difficulty = [2, 4, 6, 8, 10]; | ||
const profit = [10, 20, 30, 40, 50]; | ||
const worker = [4, 5, 6, 7]; | ||
console.log(maxProfitAssignment(difficulty, profit, worker)); // Output: 100 | ||
``` | ||
|
||
### Explanation | ||
|
||
1. **Combining and Sorting Jobs**: We first combine each job's difficulty and profit into a single array called `jobs`. Then, we sort this array based on the difficulty of the jobs. This helps us to quickly find the most profitable job a worker can do by iterating through the sorted jobs array. | ||
|
||
2. **Sorting Workers**: Sorting the workers based on their abilities allows us to assign jobs in a sequential manner, ensuring that we consider each worker's ability progressively. | ||
|
||
3. **Two-pointer Technique**: We use two pointers—one for the jobs and one for the workers. For each worker, we iterate through the jobs to find the highest profit job that the worker can handle. We maintain the best profit found so far (`bestProfit`) and add it to the total profit for each worker. | ||
|
||
### Conclusion | ||
|
||
This approach efficiently assigns the most profitable jobs to workers using sorting and a two-pointer technique, resulting in an optimized solution with a time complexity of \(O(n \log n + m \log m)\). This method ensures that we maximize the total profit by leveraging the sorted order of jobs and workers. | ||
|
||
Feel free to try out the provided code with different inputs to see how it works with various scenarios! |