Problem statement

https://binarysearch.com/problems/Task-Run/

Solution

Simulate process with time time and keep for each element its previous place.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, tasks, k):
        last, time = {}, 0
        for x in tasks:
            time = max(last.get(x, -k) + k, time) + 1
            last[x] = time

        return time