1467.Ask For Cooling Time
You have a bunch of skills that need to be released. The release order isarr
. Must be released in order. Each skill has a cooldown of lengthn
. That is, there must be an interval of at leastn
seconds between two similar skills. It takes 1 second to release each skill, return the time it takes to finish all skills.
Example
Givenarr=[1,1,2,2]
,n=2
.Return8
.
The order is [1, _, _, 1, 2, _, _, 2].So return 8.
Skill 1 is released in the 1st second, in the 2nd second and the 3rd second enters the cooling time, and the 4th second releases the second time.
Skill 2 is released in the 5th second, in the 6th second and the 7th second enters the cooling time, and the 8th second releases the second time.
Givenarr=[1,2,1,2]
,n=2
. Return5
.
The order is [1, 2, _, 1, 2].So return 5.
Skill 1 is released in the 1st second, in the 2nd second and the 3rd second enters the cooling time, and the 4th second releases the second time.
Skill 2 is released in the 2nd second, in the 3rd second and the 4th second enters the cooling time, and the 5th second releases the second time.
Similar to 621. Task Scheduler + Akuna/Drone Delivery
class Solution:
"""
@param arr: The release order
@param n: The cooldown
@return: Return the time
"""
def askForCoolingTime(self, arr, n):
# Write your code here
t = {}
T = 1
if not len(arr):
return 0
# print('arr[0]:{}; t[arr[0]]:{}'.format(arr[0], t[arr[0]]))
for i in range (1, len(arr)):
t[arr[i]] = 0
t[arr[0]] = T + n
# print('t[arr[0]]: {}'.format(t[arr[0]]))
for i in range (1, len(arr)):
# print('t[arr[1]]: {}'.format(t[arr[1]]))
if T < t[arr[i]]:
T = t[arr[i]] + 1
t[arr[i]] = T + n
# print('in i:{}; T:{} ; arr[i]: {}; t[arr[i]]: {}'.format(i,T,arr[i],t[arr[i]]))
continue
T += 1
t[arr[i]] = T + n
# print('out i:{}; T:{} ; arr[i]: {}; t[arr[i]]: {}'.format(i,T,arr[i],t[arr[i]]))
return T