Loading problem...
You are given an array of CPU operations represented by characters from 'A' to 'Z', where each character denotes a specific type of operation that needs to be executed. You are also provided with a non-negative integer cooldown representing the mandatory waiting period between two executions of the same operation type.
During each time interval, the CPU can either:
Operations can be executed in any order, but there must be at least cooldown intervals between any two operations of the same type. In other words, if operation 'A' is executed at time t, the next 'A' operation cannot be executed until time t + cooldown + 1 or later.
Your task is to determine the minimum total number of intervals (including both execution intervals and idle intervals) required to complete all the given operations while respecting the cooldown constraint.
operations = ["A","A","A","B","B","B"]
cooldown = 28One optimal execution sequence is: A → B → idle → A → B → idle → A → B. After executing operation A, we must wait 2 intervals before executing A again. The same rule applies to operation B. In the 3rd interval, neither A nor B can be executed (both are on cooldown), so the CPU idles. By the 4th interval, A becomes available again. Total intervals: 8.
operations = ["A","C","A","B","D","B"]
cooldown = 16One valid sequence is: A → B → C → D → A → B. With a cooldown of 1, we can repeat an operation after just one intervening operation. Since we have 4 distinct operation types, we can interleave them perfectly without any idle time. Total intervals: 6 (equals the number of operations).
operations = ["A","A","A","B","B","B"]
cooldown = 310One optimal sequence is: A → B → idle → idle → A → B → idle → idle → A → B. With only 2 operation types and a cooldown of 3, we cannot fill all cooldown slots with useful work. We need 2 idle intervals between each repetition of the same operation after using the other operation type. Total intervals: 10.
Constraints