Intuition

  • For each task, that element must be scheduled immediately.
  • The task cannot be scheduled if its cooldown period has not yet elapsed. Therefore, set the current time ct to the time that the task can be scheduled in again, then go to the next task.

Complexity

Runtime

Space

Code

class Solution:
    def taskSchedulerII(self, tasks: List[int], space: int) -> int:
        hm = {}
        ct = 0
        for i, task in enumerate(tasks):
            if task in hm and ct < hm[task]:
                ct = hm[task]
                
            hm[task] = ct + space + 1
            ct += 1
 
        return ct

Cards

START Basic Front: Task Scheduler Ii Back: Always set the current time to the time the given task can be scheduled in again. END


References