Skip to content

Commit 50ce35d

Browse files
Added Day-28 Solution
1 parent 878554a commit 50ce35d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Day-28_Task_Scheduler.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
You are given a char array representing tasks CPU need to do. It contains capital letters A to Z where each letter represents a different task. Tasks could be done without the original order of the array. 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.
3+
4+
However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.
5+
6+
You need to return the least number of units of times that the CPU will take to finish all the given tasks.
7+
8+
9+
10+
Example 1:
11+
12+
Input: tasks = ["A","A","A","B","B","B"], n = 2
13+
Output: 8
14+
Explanation:
15+
A -> B -> idle -> A -> B -> idle -> A -> B
16+
There is at least 2 units of time between any two same tasks.
17+
Example 2:
18+
19+
Input: tasks = ["A","A","A","B","B","B"], n = 0
20+
Output: 6
21+
Explanation: On this case any permutation of size 6 would work since n = 0.
22+
["A","A","A","B","B","B"]
23+
["A","B","A","B","A","B"]
24+
["B","B","B","A","A","A"]
25+
...
26+
And so on.
27+
Example 3:
28+
29+
Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
30+
Output: 16
31+
Explanation:
32+
One possible solution is
33+
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
34+
35+
36+
Constraints:
37+
38+
The number of tasks is in the range [1, 10000].
39+
The integer n is in the range [0, 100].
40+
41+
42+
'''
43+
44+
class Solution:
45+
def leastInterval(self, tasks: List[str], n: int) -> int:
46+
freq = collections.Counter(tasks)
47+
max_freq = max(freq.values())
48+
freq = list(freq.values())
49+
max_freq_ele_count = 0 # total_elements_with_max_freq, last row elements
50+
i = 0
51+
while( i < len(freq)):
52+
if freq[i] == max_freq:
53+
max_freq_ele_count += 1
54+
i += 1
55+
56+
ans = (max_freq - 1) * (n+1) + max_freq_ele_count
57+
58+
return max(ans, len(tasks))
59+

0 commit comments

Comments
 (0)