Skip to content

Conversation

@emcc2302
Copy link
Contributor

@emcc2302 emcc2302 commented Oct 5, 2025

Approach:

This problem combines binary search + greedy + deque simulation.
Binary Search:
We binary search on the number of tasks (mid) we can assign.
For each mid, we check if it's possible to assign the smallest mid tasks to the strongest mid workers.
Feasibility Check (canAssign):
Sort tasks and workers in ascending order.
Use a deque to track workers who can do a task with a pill (boosting their strength by strength).
Iterate over tasks (from hardest to easiest among selected mid tasks):
If a strong enough worker (without a pill) is available, assign directly.
Otherwise, push workers who can do the task with a pill into the deque.
Use a pill if needed and available; otherwise, fail.
If all tasks are assigned, return true.
Continue binary search until the maximum feasible number of tasks is found.

Intuition:

We want to maximize the number of tasks assigned by carefully distributing limited pills.
Binary search finds the upper limit, while the greedy check ensures pills are used optimally — only when weaker workers need them to complete harder tasks.
It’s like matching workers to tasks in the most efficient way possible under resource (pill) constraints.

Solution in Code (C++)
class Solution {
public:
int maxTaskAssign(vector& tasks, vector& workers, int pills, int strength) {
sort(tasks.begin(), tasks.end());
sort(workers.begin(), workers.end());

    int low = 0, high = min(tasks.size(), workers.size());

    while (low < high) {
        int mid = (low + high + 1) / 2;
        if (canAssign(tasks, workers, pills, strength, mid)) {
            low = mid;
        } else {
            high = mid - 1;
        }
    }

    return low;
}

private:
bool canAssign(const vector& tasks, const vector& workers,
int pills, int strength, int taskCount) {
deque boosted;
int w = workers.size() - 1;
int freePills = pills;

    for (int t = taskCount - 1; t >= 0; --t) {
        int task = tasks[t];

        if (!boosted.empty() && boosted.front() >= task) {
            boosted.pop_front();
        } else if (w >= 0 && workers[w] >= task) {
            --w;
        } else {
            while (w >= 0 && workers[w] + strength >= task) {
                boosted.push_back(workers[w--]);
            }
            if (boosted.empty() || freePills == 0) {
                return false;
            }
            boosted.pop_back();
            --freePills;
        }
    }

    return true;
}

};

@SjxSubham
Copy link
Owner

U and others who contributed already are only allowed to solve only 2 issue...
Let's give chance to others as well... find another Repo which are also participating If u contribute all PRs in single repo... then Hacktoberfest team will find this as spam PRs....

Afterall thanks for the PR...!! I hope u can understand give chance to others too... :-)

@SjxSubham SjxSubham added good first issue Good for newcomers hacktoberfest hacktoberfest hacktoberest-accepted hacktoberfest-accepted labels Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

good first issue Good for newcomers hacktoberest-accepted hacktoberfest-accepted hacktoberfest hacktoberfest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants