Count of Unfinished Tasks After Each Shift Problem
#4012
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 4012.
You are given two integer arrays tasks and shifts.
tasks[i]represents the time required to complete thei^thtask.shifts[j]represents the amount of time available during thej^thshift.
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.
Example 1:
Input: tasks = [1,4,4], shifts = [9,1,4]
Output: [0,2,1]
Explanation:
- Shift 0: The tasks require 1 + 4 + 4 = 9 units of time, so all tasks are completed. There are 0 unfinished tasks.
- Shift 1: Processing restarts from task 0. The shift has time 1, so task 0 is completed. There are 2 unfinished tasks.
- Shift 2: Processing continues from task 1. The shift has time 4, so task 1 is completed. There is 1 unfinished task.
Example 2:
Input: tasks = [2,3,4], shifts = [20,4,5]
Output: [0,2,0]
Explanation:
- Shift 0: The tasks require 2 + 3 + 4 = 9 units of time, so all tasks are completed. The remaining time in this shift is ignored. There are 0 unfinished tasks.
- Shift 1: Processing restarts from task 0. The shift has time 4, so task 0 is completed and task 1 is partially completed. There are 2 unfinished tasks.
- Shift 2: Processing continues from task 1. The remaining time needed is 1 + 4 = 5, so all tasks are completed. There are 0 unfinished tasks.
Example 3:
Input: tasks = [4,2], shifts = [3,6,1]
Output: [2,0,2]
Explanation:
- Shift 0: The shift has time 3, so task 0 is partially completed with 1 unit of work remaining. There are 2 unfinished tasks.
- Shift 1: Processing continues from task 0. The remaining time needed is 1 + 2 = 3, so all tasks are completed. There are 0 unfinished tasks.
- Shift 2: Processing restarts from task 0. The shift has time 1, so task 0 is partially completed. There are 2 unfinished tasks.
Constraints:
1 <= tasks.length <= 10^51 <= shifts.length <= 10^51 <= tasks[i] <= 10^91 <= shifts[i] <= 10^9
Solution
Approach: Prefix Sum + Binary Search
Build the prefix sums s of the task times, so s[i] is the time needed for the first i tasks. Keep i, the task being worked on, and cur, how much of it is already done. Simulate the shifts in order:
- If the shift is shorter than what task
istill needs (tasks[i] - cur), it only advances the task:cur += shifts[j], andm - itasks are unfinished. - Otherwise task
ifinishes andt = shifts[j] - (tasks[i] - cur)is left over. Ift >= s[m] - s[i + 1], every task is done: reset toi = 0,cur = 0, with 0 unfinished tasks. - Else binary search the task
lwhere the shift ends: the firstlwitht < s[l + 1] - s[i + 1]. Thencur = t - (s[l] - s[i + 1]),i = l, andm - ltasks are unfinished.
Complexity: time O((m + n) log m), space O(m), where m and n are the lengths of tasks and shifts.
from itertools import accumulate
class Solution:
def countTasks(self, tasks: List[int], shifts: List[int]) -> List[int]:
m, n = len(tasks), len(shifts)
s = list(accumulate(tasks, initial=0))
ans = [0] * n
i = cur = 0
for j in range(n):
if shifts[j] < tasks[i] - cur:
cur += shifts[j]
ans[j] = m - i
else:
t = shifts[j] - (tasks[i] - cur)
if t >= s[-1] - s[i + 1]:
i = cur = 0
else:
l, r = i + 1, m
while l < r:
mid = (l + r) >> 1
if t < s[mid + 1] - s[i + 1]:
r = mid
else:
l = mid + 1
cur = t - (s[l] - s[i + 1])
i = l
ans[j] = m - i
return ans