Skip to content

Commit 8b4f5ae

Browse files
authored
Generate PR for codedecks by solving any STACK/QUEUE section problem of LeetCode #90 (#107)
1 parent a63d6fd commit 8b4f5ae

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Python/621-Task-Scheduler.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task.
3+
Tasks could be done in any order. Each task is done in one unit of time. For each unit of time,
4+
the CPU could complete either one task or just be idle.
5+
6+
However, there is a non-negative integer n that represents the cooldown period between
7+
two same tasks (the same letter in the array),
8+
is that there must be at least n units of time between any two same tasks.
9+
10+
11+
Memory ->14.4MB
12+
runtime ->412ms
13+
14+
15+
16+
17+
"""
18+
19+
20+
class Solution:
21+
def leastInterval(self, tasks: List[str], n: int) -> int:
22+
counter=Counter(tasks)
23+
freq=sorted(list(counter.values()))
24+
25+
max_idle=freq.pop()
26+
total=(max_idle-1)*n
27+
28+
while freq and total>0:
29+
total=total-min(max_idle-1,freq.pop())
30+
31+
total=max(0,total)
32+
return len(tasks) + total

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
189189
| --- | ------------------------------------------------------------------------------- | --------------------------------------- | ------ | ------ | ---------- | --------------------- | ---- |
190190
| 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 |
191191
| 641 | [Design Circular Deque](https://leetcode.com/problems/design-circular-deque/) | [Java](./Java/design-circular-deque.java/) | _O(n)_ | _O(n)_ | Medium | Queue, Design |
192+
| 621 | [Task Scheduler ](https://leetcode.com/problems/task-scheduler/) | [Python](./Python/621-Task-Scheduler.py/) | _O(n)_| _O(n)_| Medium | Queue
192193

193194
<br/>
194195
<div align="right">

0 commit comments

Comments
 (0)