Skip to content

Instantly share code, notes, and snippets.

@sangheestyle
Created February 2, 2019 11:20
Show Gist options
  • Save sangheestyle/61c72d730e3aec2de4124658fabad7b8 to your computer and use it in GitHub Desktop.
Save sangheestyle/61c72d730e3aec2de4124658fabad7b8 to your computer and use it in GitHub Desktop.
leetcode: 621. Task Scheduler
# time: O(nlogk) -> O(nlog1) ~ O(n)
# space: O(26) -> O(1)
from collections import Counter
class Solution:
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
# assume: tasks: AA
# n = 1: A # A
# . n = 2: A # # A
# . n = 3: A # # # A
# ~~~~~~~ ~
# let say d1 . d2
# d1: n+1 -> (num(A) - 1) * (n+1)
# d2: num(A) - (num(A) - 1) = 1
counter = Counter(tasks)
_, max_count = counter.most_common(1)[0]
d1 = (max_count-1) * (n+1)
d2 = sum(c == max_count for c in counter.values())
return max(len(tasks), d1 + d2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment