task_scheduler
1from collections import Counter 2 3 4# @leet start 5class Solution: 6 def leastInterval(self, tasks: list[str], n: int) -> int: 7 """ 8 This question gives a list of CPU tasks, given the letters A - Z 9 And a cooling time, `n`. Each cycle allows the completion of 1 task. 10 However, you must wait `n` cycles before doing the same task again. 11 12 To solve this problem, we can use a math formula. 13 There are `max_count` - 1 occurrences, and `N + 1` refers to the 14 cycles for cooldown + 1 for the actual instruction. 15 16 So all we do is find the most frequent items, find how many of those 17 there are, and then apply the formula, or the amount of tasks without cooldown. 18 """ 19 freq = Counter(tasks) 20 max_count = max(freq.values()) 21 22 time = (max_count - 1) * (n + 1) 23 increment = sum([1 for val in freq.values() if val == max_count]) 24 25 return max(len(tasks), time + increment) 26 27 28# @leet end 29 30 31def test(): 32 assert 2 + 2 == 4
class
Solution:
6class Solution: 7 def leastInterval(self, tasks: list[str], n: int) -> int: 8 """ 9 This question gives a list of CPU tasks, given the letters A - Z 10 And a cooling time, `n`. Each cycle allows the completion of 1 task. 11 However, you must wait `n` cycles before doing the same task again. 12 13 To solve this problem, we can use a math formula. 14 There are `max_count` - 1 occurrences, and `N + 1` refers to the 15 cycles for cooldown + 1 for the actual instruction. 16 17 So all we do is find the most frequent items, find how many of those 18 there are, and then apply the formula, or the amount of tasks without cooldown. 19 """ 20 freq = Counter(tasks) 21 max_count = max(freq.values()) 22 23 time = (max_count - 1) * (n + 1) 24 increment = sum([1 for val in freq.values() if val == max_count]) 25 26 return max(len(tasks), time + increment)
def
leastInterval(self, tasks: list[str], n: int) -> int:
7 def leastInterval(self, tasks: list[str], n: int) -> int: 8 """ 9 This question gives a list of CPU tasks, given the letters A - Z 10 And a cooling time, `n`. Each cycle allows the completion of 1 task. 11 However, you must wait `n` cycles before doing the same task again. 12 13 To solve this problem, we can use a math formula. 14 There are `max_count` - 1 occurrences, and `N + 1` refers to the 15 cycles for cooldown + 1 for the actual instruction. 16 17 So all we do is find the most frequent items, find how many of those 18 there are, and then apply the formula, or the amount of tasks without cooldown. 19 """ 20 freq = Counter(tasks) 21 max_count = max(freq.values()) 22 23 time = (max_count - 1) * (n + 1) 24 increment = sum([1 for val in freq.values() if val == max_count]) 25 26 return max(len(tasks), time + increment)
This question gives a list of CPU tasks, given the letters A - Z
And a cooling time, n. Each cycle allows the completion of 1 task.
However, you must wait n cycles before doing the same task again.
To solve this problem, we can use a math formula.
There are max_count - 1 occurrences, and N + 1 refers to the
cycles for cooldown + 1 for the actual instruction.
So all we do is find the most frequent items, find how many of those there are, and then apply the formula, or the amount of tasks without cooldown.
def
test():