2365. Task Scheduler II
SourceBiweekly Contest 84 Q3DifficultyMediumRating1622
Description
You are given a 0-indexed array of positive integers tasks, representing tasks that need to be completed in order, where tasks[i] represents the type of the ith task.
You are also given a positive integer space, which represents the minimum number of days that must pass after the completion of a task before another task of the same type can be performed.
Each day, until all tasks have been completed, you must either:
- Complete the next task from
tasks, or - Take a break.
Return the minimum number of days needed to complete all tasks.
Example 1:
Input: tasks = [1,2,1,2,3,1], space = 3 Output: 9 Explanation: One way to complete all tasks in 9 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. Day 7: Take a break. Day 8: Complete the 4th task. Day 9: Complete the 5th task. It can be shown that the tasks cannot be completed in less than 9 days.
Example 2:
Input: tasks = [5,8,8,5], space = 2 Output: 6 Explanation: One way to complete all tasks in 6 days is as follows: Day 1: Complete the 0th task. Day 2: Complete the 1st task. Day 3: Take a break. Day 4: Take a break. Day 5: Complete the 2nd task. Day 6: Complete the 3rd task. It can be shown that the tasks cannot be completed in less than 6 days.
Constraints:
1 <= tasks.length <= 1051 <= tasks[i] <= 1091 <= space <= tasks.length
Solutions
Solution 1: Hash Table + Simulation
Thinking
Equal tasks need at least \(\textit{space}\) days between them and must run in the given order. \(n \le 10^5\), so we should not insert idle days on an explicit calendar.
A map stores the next allowed day of each task. Advance one day, take the max with that allowed day, then write the next allowed time as \(\textit{ans}+\textit{space}+1\).
We can use a hash table \(\textit{day}\) to record the next time each task can be executed. Initially, all values in \(\textit{day}\) are \(0\). We use a variable \(\textit{ans}\) to record the current time.
We iterate through the array \(\textit{tasks}\). For each task \(\textit{task}\), we increment the current time \(\textit{ans}\) by one, indicating that one day has passed since the last task execution. If \(\textit{day}[\textit{task}] > \textit{ans}\) at this time, it means that task \(\textit{task}\) can only be executed on the \(\textit{day}[\textit{task}]\) day. Therefore, we update the current time \(\textit{ans} = \max(\textit{ans}, \textit{day}[\textit{task}])\). Then we update the value of \(\textit{day}[\textit{task}]\) to \(\textit{ans} + \textit{space} + 1\), indicating that the next time task \(\textit{task}\) can be executed is at \(\textit{ans} + \textit{space} + 1\).
After the iteration, we return \(\textit{ans}\).
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the length of the array \(\textit{tasks}\).
1 2 3 4 5 6 7 8 9 | |
1 2 3 4 5 6 7 8 9 10 11 12 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 | |