LeetCode 2071: Maximum Number of Tasks You Can Assign Solution

Master LeetCode problem 2071 (Maximum Number of Tasks You Can Assign), a hard 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.

2071. Maximum Number of Tasks You Can Assign

Problem Explanation

Explanation:

To solve this problem, we can follow these steps:

  1. Sort the tasks and workers arrays in descending order.
  2. For each task, try to assign a worker with the highest strength possible. If a worker can't meet the task's strength requirement, check if giving them a magical pill would make them eligible.
  3. Keep track of the number of tasks that can be completed.

Time Complexity: O(nlogn) where n is the maximum of the lengths of tasks and workers arrays. Space Complexity: O(1)

:

Solution Code

class Solution {
    public int maxTasks(int[] tasks, int[] workers, int pills, int strength) {
        Arrays.sort(tasks);
        Arrays.sort(workers);
        
        int tasksCompleted = 0;
        int workerIdx = 0;
        
        for (int task : tasks) {
            while (workerIdx < workers.length && workers[workerIdx] < task) {
                if (pills > 0 && workers[workerIdx] + strength >= task) {
                    pills--;
                    workerIdx++;
                    tasksCompleted++;
                } else {
                    workerIdx++;
                }
            }
            if (workerIdx < workers.length && workers[workerIdx] >= task) {
                workerIdx++;
                tasksCompleted++;
            }
        }
        
        return tasksCompleted;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2071 (Maximum Number of Tasks You Can Assign)?

This page provides optimized solutions for LeetCode problem 2071 (Maximum Number of Tasks You Can Assign) 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 2071 (Maximum Number of Tasks You Can Assign)?

The time complexity for LeetCode 2071 (Maximum Number of Tasks You Can Assign) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2071 on DevExCode?

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

Back to LeetCode Solutions