-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy path1042.py
61 lines (55 loc) · 2 KB
/
1042.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
__________________________________________________________________________________________________
sample 168 ms submission
class Solution:
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
g = [[] for _ in range(n + 1)]
for x, y in paths:
g[max(x, y)] += min(x, y),
ret = [1] * (n + 1)
for x in range(1, n + 1):
for c in (1, 2, 3, 4):
for y in g[x]:
if ret[y] == c:
break
else:
ret[x] = c
break
# print(g)
return ret[1:]
__________________________________________________________________________________________________
sample 172 ms submission
class Solution:
def gardenNoAdj(self, N: int, paths: List[List[int]]) -> List[int]:
used = [set() for n in range(N)]
graph = [[] for n in range(N)]
for n1, n2 in paths:
if n1 > n2:
n1, n2 = n2, n1
graph[n1 - 1].append(n2 - 1)
res = [0] * N
for n in range(N):
for c in range(1, 5):
if c not in used[n]:
break
res[n] = c
for n1 in graph[n]:
used[n1].add(c)
# print(graph, used)
return res
__________________________________________________________________________________________________
sample 176 ms submission
class Solution:
def gardenNoAdj(self, N: int, paths: List[List[int]]) -> List[int]:
g = [[] for _ in range(N + 1)]
flowers = [0] * (N + 1)
for p in paths:
fr, to = p
g[fr].append(to)
g[to].append(fr)
for i in range(1, len(g)):
choices = {1, 2, 3, 4}
for neighbor in g[i]:
if flowers[neighbor] in choices:
choices.remove(flowers[neighbor])
flowers[i] = choices.pop()
return flowers[1:]