LeetCode 3476: Maximize Profit from Task Assignment Solution

Master LeetCode problem 3476 (Maximize Profit from Task Assignment), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

3476. Maximize Profit from Task Assignment

Problem Explanation

Explanation:

To maximize the profit from task assignment, we can first sort the tasks and then assign the tasks to the workers with the highest and lowest efficiency alternatively. This way, we can ensure that the total profit is maximized by choosing the most efficient worker for each task.

  1. Sort the tasks based on their difficulty.
  2. Sort the workers based on their efficiency.
  3. Iterate over the sorted tasks and assign each task to the worker with the highest efficiency available.
  4. Calculate the total profit as the sum of the products of task difficulties and worker efficiencies.

Time complexity: O(n log n) where n is the number of tasks or workers. Space complexity: O(n) for storing the sorted tasks or workers.

:

Solution Code

import java.util.Arrays;

class Solution {
    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int n = difficulty.length;
        int maxProfit = 0;
        
        int[][] tasks = new int[n][2];
        for (int i = 0; i < n; i++) {
            tasks[i][0] = difficulty[i];
            tasks[i][1] = profit[i];
        }
        
        Arrays.sort(tasks, (a, b) -> a[0] - b[0]);
        Arrays.sort(worker);
        
        int i = 0, best = 0;
        for (int w : worker) {
            while (i < n && tasks[i][0] <= w) {
                best = Math.max(best, tasks[i][1]);
                i++;
            }
            maxProfit += best;
        }
        
        return maxProfit;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3476 (Maximize Profit from Task Assignment)?

This page provides optimized solutions for LeetCode problem 3476 (Maximize Profit from Task Assignment) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 3476 (Maximize Profit from Task Assignment)?

The time complexity for LeetCode 3476 (Maximize Profit from Task Assignment) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3476 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3476 in Java, C++, or Python.

Back to LeetCode Solutions