Skip to content

Conversation

@Subhosjx
Copy link
Contributor

@Subhosjx Subhosjx commented Oct 16, 2025

Intuition

We need to assign the maximum number of tasks to workers. Each worker has a strength, and we can give up to pills to temporarily increase their strength. The best strategy is to try assigning more tasks and check feasibility using a greedy approach.

Approach

  1. Sort tasks and workers in ascending order to make assignments easier.
  2. Use Binary Search to maximize the number of tasks we can assign (k).
  3. Check Feasibility (_canAssign function) for each k:
  •   Pick the k strongest workers.
    
  •  Try assigning the k easiest tasks (reverse order).
    
  •  Use a balanced BST (SplayTreeMap) to track available workers.
    
  •  If the strongest worker can do a task → Assign it.
    
  •  Else, if a worker is close, give a pill and assign the task.
    
  •  Stop if pills run out before assigning all k tasks.
    
  1. Update Binary Search:
    - If k is possible, try a larger k.
    - If not, reduce k.
  2. Return the largest valid k.

Code Solution (C++)

    // Your code goes here
    class Solution {
public:
  bool check(vector<int>& tasks, vector<int>& workers, int pills, int strength, int mid) {
        int pillsUsed = 0;
        multiset<int> st(begin(workers), begin(workers) + mid); //best mid workers

        for(int i = mid-1; i >= 0; i--) {
            int reqrd = tasks[i];
            auto it   = prev(st.end());

            if(*it >= reqrd) {
                st.erase(it);
            } else if(pillsUsed >= pills) {
                return false;
            } else {
                //find the weakest worker which can do this strong task using pills
                auto weakestWorkerIt = st.lower_bound(reqrd - strength);
                if(weakestWorkerIt == st.end()) {
                    return false;
                }
                st.erase(weakestWorkerIt);
                pillsUsed++;
            }
        }

        return true;
    }
    int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {
        int m = tasks.size();
        int n = workers.size();

        int l = 0;
        int r = min(m, n);

        sort(begin(tasks), end(tasks));
        sort(begin(workers), end(workers), greater<int>());

        int result = 0;

        while(l <= r) {
            int mid = l + (r-l)/2;

            if(check(tasks, workers, pills, strength, mid)) {
                result = mid;
                l = mid+1;
            } else {
                r = mid-1;
            }
        }

        return result;

   
    }
};

Related Issues

By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Summary by Sourcery

New Features:

  • Add Solution class with maxTaskAssign method that uses binary search to determine the maximum number of tasks and a check helper that performs greedy assignment with a multiset and pill usage

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 16, 2025

Reviewer's Guide

This PR adds a C++ solution for LeetCode problem 2071 by sorting tasks and workers, using a binary search over the number of tasks to assign, and a multiset-based greedy check function that allocates pills to boost workers’ strength when needed.

Sequence diagram for the task assignment process with pills

sequenceDiagram
  participant Solution
  participant "tasks[]"
  participant "workers[]"
  participant "multiset st"
  participant "pills"
  Solution->>tasks[]: Sort tasks ascending
  Solution->>workers[]: Sort workers descending
  loop Binary search for k (max tasks)
    Solution->>multiset st: Select k strongest workers
    loop For each of k easiest tasks (reverse order)
      Solution->>multiset st: Find strongest available worker
      alt Worker strength >= task requirement
        Solution->>multiset st: Assign task, remove worker
      else Pills available
        Solution->>multiset st: Find weakest worker that can do task with pill
        Solution->>pills: Use pill
        Solution->>multiset st: Assign task, remove worker
      else No worker can do task
        Solution->>Solution: Return false for this k
      end
    end
    Solution->>Solution: If all tasks assigned, k is feasible
  end
  Solution->>Solution: Return largest feasible k
Loading

Class diagram for Solution and helper methods

classDiagram
  class Solution {
    +int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength)
    -bool check(vector<int>& tasks, vector<int>& workers, int pills, int strength, int mid)
  }
  Solution : check
  Solution : maxTaskAssign
Loading

File-Level Changes

Change Details Files
Implement maxTaskAssign with binary search and greedy feasibility check using multiset
  • Introduce check method utilizing a multiset to assign tasks and track pills usage
  • Pre-sort tasks in ascending order and workers in descending order before searching
  • Perform binary search over possible assignment counts, invoking check to adjust bounds and record the maximum
2071. Maximum Number of Tasks You Can Assign.cpp

Possibly linked issues

  • #2071: The PR adds the intuition, approach, and C++ code solution for the 'Maximum Number of Tasks You Can Assign' problem, completing the issue's tasks.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 16, 2025
@SjxSubham SjxSubham linked an issue Oct 16, 2025 that may be closed by this pull request
4 tasks
@SjxSubham SjxSubham merged commit ef6d6c7 into SjxSubham:main Oct 16, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2071. Maximum Number of Tasks You Can Assign

2 participants