Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. class Solution:
  4. def leastInterval(self, tasks: List[str], n: int) -> int:
  5. if not tasks:
  6. return 0
  7.  
  8.  
  9. task_dict = defaultdict(int)
  10. for task in tasks:
  11. task_dict[task] += 1
  12.  
  13.  
  14. task_list = [val for val in task_dict.values()]
  15. task_list.sort(reverse=True)
  16. print(task_list)
  17. total = 0
  18. count = -1
  19. reset = True
  20. del_list = []
  21. while task_list:
  22.  
  23. task_list.sort(reverse=True)
  24. # for i in range(0,len(task_list)-1):
  25. # if task_list[i]<task_list[i+1]:
  26. # task_list[i], task_list[i+1] = task_list[i+1], task_list[i]
  27. # else:
  28. # break
  29.  
  30. if not reset:
  31. total += (n-count)
  32. count = -1
  33.  
  34. reset = False
  35. for i in range(0,len(task_list)):
  36. task_list[i] -= 1
  37.  
  38. if task_list[i] == 0:
  39. del_list.append(i)
  40.  
  41. count += 1
  42. total += 1
  43. if count == n:
  44. count = -1
  45. reset = True
  46. break
  47.  
  48. while del_list:
  49. task_list.pop(del_list.pop())
  50.  
  51. return total
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement