From 57aeb56954fde41a5cdd00ee9e54d5f75dce8290 Mon Sep 17 00:00:00 2001 From: Nishant Nirmal Date: Wed, 18 Mar 2020 16:39:49 +0530 Subject: [PATCH] Task Scheduler --- 0621-Task-Scheduler.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 0621-Task-Scheduler.py diff --git a/0621-Task-Scheduler.py b/0621-Task-Scheduler.py new file mode 100644 index 0000000..86702d7 --- /dev/null +++ b/0621-Task-Scheduler.py @@ -0,0 +1,38 @@ +''' +Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks. Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle. + +However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. + +You need to return the least number of intervals the CPU will take to finish all the given tasks. + + + +Example: + +Input: tasks = ["A","A","A","B","B","B"], n = 2 +Output: 8 +Explanation: A -> B -> idle -> A -> B -> idle -> A -> B. + + +Note: + +The number of tasks is in the range [1, 10000]. +The integer n is in the range [0, 100]. +''' +class Solution: + def leastInterval(self, tasks: List[str], n: int) -> int: + d = {} + max_freq = max_count = 0 + for c in tasks: + if c in d: d[c] += 1 + else: d[c] = 1 + if d[c] > max_freq: + max_freq = d[c] + max_count = 1 + elif d[c] == max_freq: max_count += 1 + + res = (max_freq - 1) * (n + 1) + max_count + if len(d) <= n + 1: + return res + else: + return res if res > len(tasks) else len(tasks)