Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Maximum Number of Tasks You Can Assign

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2071" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You have n tasks and m workers. Each task has a strength requirement stored in a 0-indexed integer array tasks, with the ith task requiring tasks[i] strength to complete. The strength of each worker is stored in a 0-indexed integer array workers, with the jth worker having workers[j] strength. Each worker can only be assigned to a single task and must have a strength greater than or equal to the task's strength requirement (i.e., workers[j] >= tasks[i]). Additionally, you have pills magical pills that will increase a worker's strength by strength. You can decide which workers receive the magical pills, however, you may only give each worker at most one magical pill. Given the 0-indexed integer arrays tasks and workers and the integers pills and strength, return the maximum number of tasks that can be completed. Example 1: Input: tasks = [3,2,1], workers = [0,3,3], pills = 1, strength = 1 Output: 3 Explanation: We can assign the magical pill and tasks as follows: - Give the magical pill to worker 0. - Assign worker 0 to task 2 (0 + 1 >= 1) - Assign worker 1 to task 1 (3 >= 2) - Assign worker 2 to task 0 (3 >= 3) Example 2: Input: tasks = [5,4], workers = [0,0,0], pills = 1, strength = 5 Output: 1 Explanation: We can assign the magical pill and tasks as follows: - Give the magical pill to worker 0. - Assign worker 0 to task 0 (0 + 5 >= 5) Example 3: Input: tasks = [10,15,30], workers = [0,10,10,10,10], pills = 3, strength = 10 Output: 2 Explanation: We can assign the magical pills and tasks as follows: - Give the magical pill to worker 0 and worker 1. - Assign worker 0 to task 0 (0 + 10 >= 10) - Assign worker 1 to task 1 (10 + 10 >= 15) The last pill is not given because it will not make any worker strong enough for the last task. Constraints: n == tasks.length m == workers.length 1 <= n, m <= 5 * 104 0 <= pills <= m 0 <= tasks[i], workers[j], strength <= 109

Explanation

Here's the breakdown of the approach, complexity, and the Python code:

  • Approach:

    • Sort both tasks and workers arrays. Sorting tasks allows us to greedily try to fulfill the hardest tasks first. Sorting workers allows us to efficiently use binary search (or a two-pointer technique) to find suitable workers.
    • Binary search on the number of tasks that can be completed. For a given number of tasks to complete, check if it is possible to complete the x hardest tasks. We try to assign the strongest workers to the hardest tasks. If a worker is not strong enough, use a pill if available, and if still not strong enough, this number of tasks is not possible.
  • Complexity:

    • Runtime: O(n log n + m log m + n log n) dominated by sorting and binary search on the task array length.
    • Storage: O(n) in worst case for the sorted worker indices.

Code

    def max_tasks(tasks, workers, pills, strength):
    """
    Calculates the maximum number of tasks that can be completed.

    Args:
        tasks: A list of integers representing the strength required for each task.
        workers: A list of integers representing the strength of each worker.
        pills: The number of magical pills available.
        strength: The strength increase provided by each pill.

    Returns:
        The maximum number of tasks that can be completed.
    """

    def possible(num_tasks):
        """
        Checks if it's possible to complete the 'num_tasks' hardest tasks.
        """
        if num_tasks > len(tasks) or num_tasks > len(workers):
            return False

        req_tasks = sorted(tasks[-num_tasks:])  # Get the 'num_tasks' hardest tasks
        available_workers = sorted(workers)  # Workers we can use

        used_pills = 0
        worker_indices = list(range(len(workers)))
        worker_indices = sorted(worker_indices, key=lambda i: workers[i])
        worker_indices = worker_indices[len(workers) - num_tasks:]
        worker_indices.reverse()

        for task_strength in reversed(req_tasks):
            worker_found = False
            for i in range(len(worker_indices)):
                worker_index = worker_indices[i]
                if available_workers[worker_index] >= task_strength:
                    worker_found = True
                    del worker_indices[i]
                    break

            if not worker_found:
                if used_pills < pills:
                    used_pills += 1
                    for i in range(len(worker_indices)):
                        worker_index = worker_indices[i]
                        if available_workers[worker_index] + strength >= task_strength:
                            worker_found = True
                            del worker_indices[i]
                            break

                if not worker_found:
                    return False

        return True

    tasks.sort()
    workers.sort()

    left = 0
    right = min(len(tasks), len(workers))
    ans = 0

    while left <= right:
        mid = (left + right) // 2
        if possible(mid):
            ans = mid
            left = mid + 1
        else:
            right = mid - 1

    return ans

More from this blog

C

Chatmagic blog

2894 posts