[LeetCode] Count of Unfinished Tasks After Each Shift

4012. Count of Unfinished Tasks After Each Shift

You are given two integer arrays tasks and shifts.

  • tasks[i] represents the time required to complete the i_th task.
  • shifts[j] represents the amount of time available during the j^th shift.

The tasks must be processed in order from left to right.

  • Carry-over: If a task is not completed during a shift, processing continues from the same point in that task during the next shift.
  • Restart: If all tasks are completed during a shift, the shift ends immediately. Any unused time in that shift is discarded, and the next shift begins again from task 0.

A task is unfinished if it has not been fully completed. This includes a task that is currently in progress.

Return an integer array ans where ans[j] is the number of unfinished tasks immediately after the j^th shift.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
vector<int> countTasks(vector<int>& tasks, vector<int>& shifts) {
map<long long,int> ord{{0,tasks.size()}};
for(long long i = 0, c = 0; i < tasks.size(); i++) {
c += tasks[i];
ord[c] = tasks.size() - i - 1;
}
vector<int> res;
long long t = 0;
for(auto& s : shifts) {
t += s;
res.push_back(prev(ord.upper_bound(t))->second);
if(res.back() == 0) t = 0;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/count-of-unfinished-tasks-after-each-shift/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.