| 15 | ''' |
| 16 | from collections import defaultdict |
| 17 | class Solution: |
| 18 | def gardenNoAdj(self, N, paths): |
| 19 | |
| 20 | #Create Graph |
| 21 | graph = defaultdict(list) |
| 22 | for i in paths: |
| 23 | graph[i[0]].append(i[1]) #Since Graph is Bidirectional, we need to add edge both sides |
| 24 | graph[i[1]].append(i[0]) |
| 25 | flowers = [] |
| 26 | for i in range(1, N + 1): #We will traverse every node one by one |
| 27 | choice = [1, 2, 3, 4] #create a fresh new list of choices of flowers |
| 28 | for k in graph[i]: #check its neighbours |
| 29 | try: |
| 30 | choice.remove(flowers[k-1]) #if any of the neighbour has been assigned flower (say flower 'X') we will remove that flower ('X') |
| 31 | except: |
| 32 | pass |
| 33 | flowers.append(choice[0]) #Select the first flower from the remaining choices |
| 34 | return flowers |
nothing calls this directly
no outgoing calls
no test coverage detected