1727. Ordered Cooldown Days

Easy1.5 points estimatedHash TableSimulationGreedy

A workshop must complete chores in exactly the given order. The array tasks lists each chore's type, and the workshop performs at most one chore per day, starting on day 1. To let the tools cool, if a chore of some type was last done on day d, the next chore of that same type may not run before day d + space + 1, so at least space days must pass in between. When a chore cannot run yet, the workshop simply idles.

Each chore runs on the earliest day allowed. Return the day on which the final chore runs, as a long. Remember each type's last day in a hash map: O(n) time and O(n) space.

Example 1

Input:
tasks = [1,2,1,2], space = 2
Output:
5
Explanation:

Days 1 and 2 run the first two tasks, the second type-1 task waits until day 4, and the last type-2 task runs on day 5.

Example 2

Input:
tasks = [7,7,7], space = 1
Output:
5
Explanation:

The same type must be one idle day apart, so the tasks run on days 1, 3 and 5: 5.

Example 3

Input:
tasks = [1,2,3], space = 5
Output:
3
Explanation:

All types differ, so no waiting is needed and the last task runs on day 3.

Constraints

1 ≤ n ≤ 1000

1 ≤ tasks[i] ≤ 109

1 ≤ space ≤ 1000

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…