diff --git a/Python/621-Task-Scheduler.py b/Python/621-Task-Scheduler.py new file mode 100644 index 00000000..730585af --- /dev/null +++ b/Python/621-Task-Scheduler.py @@ -0,0 +1,32 @@ +""" +Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. +Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, +the CPU could complete either one task or just be idle. + +However, there is a non-negative integer n that represents the cooldown period between +two same tasks (the same letter in the array), +is that there must be at least n units of time between any two same tasks. + + +Memory ->14.4MB +runtime ->412ms + + + + +""" + + +class Solution: + def leastInterval(self, tasks: List[str], n: int) -> int: + counter=Counter(tasks) + freq=sorted(list(counter.values())) + + max_idle=freq.pop() + total=(max_idle-1)*n + + while freq and total>0: + total=total-min(max_idle-1,freq.pop()) + + total=max(0,total) + return len(tasks) + total diff --git a/README.md b/README.md index c2492d72..64090209 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | --- | ------------------------------------------------------------------------------- | --------------------------------------- | ------ | ------ | ---------- | --------------------- | ---- | | 933 | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [C++](./C++/Number-of-Recent-Calls.cpp) | _O(1)_ | _O(1)_ | Easy | Queue, Sliding Window | | 641 | [Design Circular Deque](https://leetcode.com/problems/design-circular-deque/) | [Java](./Java/design-circular-deque.java/) | _O(n)_ | _O(n)_ | Medium | Queue, Design | +| 621 | [Task Scheduler ](https://leetcode.com/problems/task-scheduler/) | [Python](./Python/621-Task-Scheduler.py/) | _O(n)_| _O(n)_| Medium | Queue