---
# System prepended metadata

title: You have N tasks to run in any order you want.Each task takes one time step to run. There must be a minimum of K (cooldown value) time steps between two runs of the same task. Write a function that returns the sequence of tasks to run in a minimal amount of time steps.

---


# You have N tasks to run in any order you want.Each task takes one time step to run. There must be a minimum of K (cooldown value) time steps between two runs of the same task. Write a function that returns the sequence of tasks to run in a minimal amount of time steps.

# 1) tasks = ["A", "A", "B"], K = 3 -> ["A", "B", "", "", "A"]
# 2) tasks = ["A","A","B","B","C", "C"], K = 1 -> ["A", "B", "C", "A", "B", "C"]
# 3) tasks = ["A", "B", "B", "C"], K = 4 -> ["B", "A", "C", "", "", "B"]

``` python
from collections import Counter

frequency_dict = Counter(tasks)

cooldown_dict = {t: 0 for t in set(tasks)}




output = []

while all(frequency_dict.values() <= 0):
    max_freq = -1
    curr_task = ""
    
    for t, f in frequency_dict.items():
        if f > max_freq and cooldown_dict[t] <= 0: 
            curr_task = t
            max_freq = f

    cooldown_dict = {t: c-1 for t, c in cooldown_dict.items()}
    
    if curr_task:
        cooldown_dict[curr_task] = K
        frequency_dict[curr_task] -= 1

    output.append(t)
    

    
    

