Skip to content

Commit b0af5e3

Browse files
authored
Create add-edges-to-make-degrees-of-all-nodes-even.py
1 parent 6529c99 commit b0af5e3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
# graph
5+
class Solution(object):
6+
def isPossible(self, n, edges):
7+
"""
8+
:type n: int
9+
:type edges: List[List[int]]
10+
:rtype: bool
11+
"""
12+
adj = [set() for _ in xrange(n)]
13+
for u, v in edges:
14+
adj[u-1].add(v-1)
15+
adj[v-1].add(u-1)
16+
odds = [u for u in xrange(n) if len(adj[u])%2]
17+
if len(odds) == 0:
18+
return True
19+
if len(odds) == 2:
20+
return any(odds[0] not in adj[u] and odds[1] not in adj[u] for u in range(n))
21+
if len(odds) == 4:
22+
return ((odds[0] not in adj[odds[1]] and odds[2] not in adj[odds[3]]) or
23+
(odds[0] not in adj[odds[2]] and odds[1] not in adj[odds[3]]) or
24+
(odds[0] not in adj[odds[3]] and odds[1] not in adj[odds[2]]))
25+
return False

0 commit comments

Comments
 (0)