Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c80ba45

Browse files
committed
Add greedy algorithm
This is much more efficient and is easy to understand.
1 parent ea7121a commit c80ba45

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

python/0621-task-scheduler.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,17 @@ def leastInterval(self, tasks: List[str], n: int) -> int:
1818
if q and q[0][1] == time:
1919
heapq.heappush(maxHeap, q.popleft()[0])
2020
return time
21+
22+
23+
# Greedy algorithm
24+
class Solution(object):
25+
def leastInterval(self, tasks: List[str], n: int) -> int:
26+
counts = collections.Counter(tasks)
27+
max_count = max(counts.values())
28+
min_time = (max_count - 1) * (n + 1)
29+
30+
for count in counts.values():
31+
if count == max_count:
32+
min_time += 1
33+
34+
return max(min_time, len(tasks))

0 commit comments

Comments
 (0)