Task Scheduler II
Your flashcards come up in a fixed order: tasks lists the id of the card for each review, in that order, and the list is never reordered. A day holds at most one review, always of the next card in line. A card you have just reviewed must rest for space whole days before the same id may be reviewed again, and the cards behind it wait their turn. Take a day off only when the next card is still resting. Days are numbered from 1. Return the number of the day on which the last review happens. Limits: 1 <= tasks.length <= 20000, 1 <= tasks[i] <= 1000000000, 1 <= space <= 20000. The answer always fits in a 32-bit int. Example: tasks = [3,8,3,3,8], space = 2 => 8 day: 1 2 3 4 5 6 7 8 card: 3 8 - 3 - - 3 8 (- is a day off) Card 3 is reviewed on days 1, 4 and 7: two resting days in between each time.
Your flashcards come up in a fixed order, one review a day at most, and a card you just saw has to rest for a set number of full days before it can return. On which day does the last review happen, if you take a day off only while the next card is still resting? Interviewers like it because the whole problem hides in that rest, and the classic trap is counting it one day short or one day long. Expect up to 20,000 reviews, card ids up to a billion, and a rest of 1 to 20,000 days.
Examples
You write
task_scheduler_ii(tasks: List[int], space: int)
Example 1
- Input
- tasks = [3,8,3,3,8], space = 2
- returns
- 8
Example 2
- Input
- tasks = [4,9,2,7,5], space = 6
- returns
- 5
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.