Approach
Sorting and greedy selection
For Minimum Time to Complete All Tasks, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 31 lines of C++ from the credited upstream file 2589.cpp.
- The implementation visibly relies on sequence storage.
- 2 loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 int findMinimumTime(vector<vector<int>>& tasks) {4 constexpr int kMax = 2000;5 vector<bool> running(kMax + 1);6 7 8 sort(9 tasks.begin(), tasks.end(),10 [](const vector<int>& a, const vector<int>& b) { return a[1] < b[1]; });11 12 for (const vector<int>& task : tasks) {13 const int start = task[0];14 const int end = task[1];15 const int duration = task[2];16 int neededDuration = duration - count(running.begin() + start,17 running.begin() + end + 1, true);18 19 20 for (int i = end; neededDuration > 0; --i) {21 if (!running[i]) {22 running[i] = true;23 --neededDuration;24 }25 }26 }27 28 return ranges::count(running, true);29 }30};31