Advertisement

Most Profit Assigning Work - LeetCode 826 Solution

Most Profit Assigning Work - Complete Solution Guide

Most Profit Assigning Work is LeetCode problem 826, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have n jobs and m workers. You are given three arrays: difficulty , profit , and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the i th job, and worker[j] is the ability of j th worker (i.e., the j th worker can only complete a job with difficulty at most worker[j] ). Every worker can be assigned at most one job , but one job can be completed multiple times . For example, if three workers attempt the same job that pays $1 , then the total profit will be $3 .

Detailed Explanation

The problem asks us to assign jobs to workers to maximize the total profit. Each job has a difficulty and a profit associated with it. Each worker has an ability, meaning they can only perform jobs with a difficulty level less than or equal to their ability. A worker can only be assigned at most one job, but a job can be completed multiple times by different workers. The goal is to find the maximum possible total profit by optimally assigning jobs to workers.

Solution Approach

The solution sorts the jobs by difficulty and the workers by ability. It then iterates through the sorted workers. For each worker, it finds the most profitable job that the worker can do (i.e., the job's difficulty is less than or equal to the worker's ability). This is done by keeping track of the maximum profit seen so far from the jobs that the current worker can do. Once the most profitable job is found, its profit is added to the total profit. This greedy approach works because sorting allows us to efficiently find the best job for each worker without having to check all possible jobs for each worker.

Step-by-Step Algorithm

  1. Step 1: Create an array of job pairs (difficulty, profit).
  2. Step 2: Sort the job array by difficulty in ascending order.
  3. Step 3: Sort the worker array in ascending order.
  4. Step 4: Initialize total profit to 0.
  5. Step 5: Initialize maximum profit to 0 (to track the max profit available for current worker).
  6. Step 6: Initialize job index to 0.
  7. Step 7: Iterate through the sorted worker array.
  8. Step 8: For each worker, iterate through the sorted job array (starting from the last checked job index) to find the maximum profit job that worker can do.
  9. Step 9: Update maximum profit with the maximum profit among jobs worker can do.
  10. Step 10: Add the maximum profit to total profit for that worker's assignment.
  11. Step 11: Return total profit.

Key Insights

  • Insight 1: Sorting the jobs by difficulty and the workers by ability allows for an efficient, greedy approach.
  • Insight 2: Maintaining a running maximum profit of jobs that a worker *can* do is crucial to maximizing profit.
  • Insight 3: The problem can be solved by iterating through the sorted workers and, for each worker, finding the most profitable job they are capable of doing.

Complexity Analysis

Time Complexity: O(nlogn + mlogm)

Space Complexity: O(n)

Topics

This problem involves: Array, Two Pointers, Binary Search, Greedy, Sorting.

Companies

Asked at: DoorDash, NetEase.