MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

Python/1042_FlowerPlantingwithNoAdjacent.py:17–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15'''
16from collections import defaultdict
17class 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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected