Problem solution · C++

Maximum Number of Tasks You Can Assign

Maximum Number of Tasks You Can Assign: a C++ solution using binary search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Binary search
Source
walkccc LeetCode Solutions
Length
57 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Binary search

For Maximum Number of Tasks You Can Assign, the implementation exploits a monotonic condition to discard half of the remaining search space after every check.

  1. Identify the ordered answer range or sorted search domain.
  2. Write a predicate whose truth changes only once.
  3. Move the appropriate boundary after each midpoint check and return the final feasible position.

Code notes

  • 57 lines of C++ from the credited upstream file 2071.cpp.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • 3 loop blocks detected.

Complexity

Multiply the logarithmic number of midpoint checks by the cost of one predicate evaluation.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeMaximum Number of Tasks You Can Assign · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills,                    int strength) {    int ans = 0;    int l = 0;    int r = min(tasks.size(), workers.size());     ranges::sort(tasks);    ranges::sort(workers);     // Returns true if we can finish k tasks.    auto canComplete = [&](int k, int pillsLeft) {      // k strongest workers      map<int, int> sortedWorkers;      for (int i = workers.size() - k; i < workers.size(); ++i)        ++sortedWorkers[workers[i]];       // Out of the k smallest tasks, start from the biggest one.      for (int i = k - 1; i >= 0; --i) {        // Find the first worker that has strength >= tasks[i].        auto it = sortedWorkers.lower_bound(tasks[i]);        if (it != sortedWorkers.end()) {          if (--(it->second) == 0)            sortedWorkers.erase(it);        } else if (pillsLeft > 0) {          // Find the first worker that has strength >= tasks[i] - strength.          it = sortedWorkers.lower_bound(tasks[i] - strength);          if (it != sortedWorkers.end()) {            if (--(it->second) == 0)              sortedWorkers.erase(it);            --pillsLeft;          } else {            return false;          }        } else {          return false;        }      }       return true;    };     while (l <= r) {      const int m = (l + r) / 2;      if (canComplete(m, pills)) {        ans = m;        l = m + 1;      } else {        r = m - 1;      }    }     return ans;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗