Skip to content

Commit d0f61db

Browse files
authored
Create flower-planting-with-no-adjacent.py
1 parent 1817e40 commit d0f61db

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def gardenNoAdj(self, N, paths):
6+
"""
7+
:type N: int
8+
:type paths: List[List[int]]
9+
:rtype: List[int]
10+
"""
11+
result = [0]*N
12+
G = [[] for i in xrange(N)]
13+
for x, y in paths:
14+
G[x-1].append(y-1)
15+
G[y-1].append(x-1)
16+
for i in xrange(N):
17+
result[i] = ({1, 2, 3, 4} - {result[j] for j in G[i]}).pop()
18+
return result

0 commit comments

Comments
 (0)