Skip to content

Commit

Permalink
Add 621
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyannn committed Mar 29, 2022
1 parent c2907cd commit e890c26
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lc_0621_task_scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from collections import Counter
from itertools import count
from typing import List
import unittest


class Solution:
def leastInterval(self, tasks: List[str], n: int) -> int:

task_list = [(v, 0) for v in Counter(tasks).values()]

for time in count():
if not task_list:
return time

tx, ix = max(((t, i) for i, (t, b) in enumerate(task_list) if time >= b),
default=(None, None))

if ix is not None:
task_list[ix: ix + 1] = [(tx - 1, time + n + 1)] if tx > 1 else []


class MyTestCase(unittest.TestCase):
def setUp(self) -> None:
self.f = Solution().leastInterval

def test_examples(self):
assert self.f(["A", "A", "A", "B", "B", "B"], 2) == 8
assert self.f(["A", "A", "A", "B", "B", "B"], 0) == 6
assert self.f(["A", "A", "A", "A", "A", "A", "B", "C", "D", "E", "F", "G"], 2) == 16

def test_simple(self):
assert self.f(["A"], 100) == 1
assert self.f(["A", "A"], 100) == 102


if __name__ == "__main__":
unittest.main()

0 comments on commit e890c26

Please sign in to comment.