Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class Solution:
  2. def gardenNoAdj(self, N: int, paths: List[List[int]]) -> List[int]:
  3. adjacency_list = []
  4. for i in range(N):
  5. adjacency_list.append(list())
  6. for edge in paths:
  7. adjacency_list[edge[0]-1].append(edge[1]-1)
  8. adjacency_list[edge[1]-1].append(edge[0]-1)
  9.  
  10. #print(adjacency_list)
  11.  
  12. colors = []
  13. current_color = 1
  14. for i in range(N):
  15. colors.append(-1)
  16. #print(colors)
  17. for k, vertices in enumerate(adjacency_list):
  18. if colors[k] == -1:
  19. used = []
  20. for vertice in vertices:
  21. #print(k, vertice, colors[vertice], '==', current_color)
  22. used.append(colors[vertice])
  23. while current_color in used:
  24. current_color += 1
  25. if current_color > 4:
  26. current_color = 1
  27. #print(k, 'final', current_color)
  28. colors[k] = current_color
  29. #print(colors)
  30. return colors
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement