Skip to content

Commit c198584

Browse files
author
Chris Wu
committed
no message
1 parent 216d4af commit c198584

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

problems/evaluate-division.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,42 @@ def addEdge(n1, n2, val):
5656

5757
return [findVal(query) for query in queries]
5858

59+
60+
from collections import defaultdict
61+
62+
class Solution(object):
63+
def calcEquation(self, equations, values, queries):
64+
#using DFS
65+
def find_val(n1, n2):
66+
if n1 not in graph or n2 not in graph: return -1
67+
if n1==n2: return 1
68+
if n2 in graph[n1]: return graph[n1][n2]
69+
70+
stack = [(n1, 1)]
71+
visited = set()
72+
73+
while stack:
74+
n, v = stack.pop() #v is the val of n1/n
75+
76+
if n in visited: continue
77+
visited.add(n)
78+
79+
if n==n2:
80+
graph[n1][n2] = v
81+
return v
82+
83+
stack.extend([(next_n, v*graph[n][next_n]) for next_n in graph[n]])
84+
return -1
85+
86+
#build graph
87+
graph = defaultdict(dict)
88+
for i, v in enumerate(values):
89+
n1, n2 = equations[i][0], equations[i][1]
90+
graph[n1][n2] = v
91+
graph[n2][n1] = 1/v
92+
93+
ans = []
94+
for n1, n2 in queries:
95+
ans.append(find_val(n1, n2))
96+
97+
return ans

problems/find-eventual-safe-states.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from collections import defaultdict, deque
2+
3+
class Solution(object):
4+
def eventualSafeNodes(self, graph):
5+
inbounds = defaultdict(list)
6+
outbondsCounter = defaultdict(int)
7+
q = deque()
8+
ans = []
9+
10+
for n, nei_list in enumerate(graph):
11+
outbondsCounter[n] = len(nei_list)
12+
for nei in nei_list:
13+
inbounds[nei].append(n)
14+
15+
for n in outbondsCounter:
16+
if outbondsCounter[n]==0:
17+
q.append(n)
18+
19+
while q:
20+
n = q.popleft()
21+
22+
for nei in inbounds[n]:
23+
outbondsCounter[nei] -= 1
24+
if outbondsCounter[nei]==0:
25+
q.append(nei)
26+
27+
ans.append(n)
28+
29+
return ans.sort()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from collections import defaultdict
2+
3+
class Solution(object):
4+
def equationsPossible(self, equations):
5+
def isConnected(n1, n2):
6+
if n1==n2: return True
7+
8+
stack = [n1]
9+
visited = set()
10+
11+
while stack:
12+
n = stack.pop()
13+
if n in visited: continue
14+
visited.add(n)
15+
16+
if n==n2: return True
17+
18+
stack.extend(graph[n])
19+
return False
20+
21+
not_equals = set()
22+
graph = defaultdict(list)
23+
24+
#build graph
25+
for e in equations:
26+
if e[1:3]=='==':
27+
graph[e[0]].append(e[3])
28+
graph[e[3]].append(e[0])
29+
elif e[1:3]=='!=':
30+
if (e[0], e[3]) not in not_equals and (e[3], e[0]) not in not_equals:
31+
not_equals.add((e[0], e[3]))
32+
33+
#find contradiction
34+
for n1, n2 in not_equals:
35+
if isConnected(n1, n2): return False
36+
37+
return True
38+
39+
"""
40+
For each `!=` equation, if we find two variables are equal (`isConnected()`), we return `False`.
41+
Otherwise, we return `True`.
42+
43+
[build graph]
44+
First, lets build the graph.
45+
Variables in the same graph means they are all equal.
46+
47+
[find contradiction]
48+
Second, we find if any "!=" variables (`n1` and `n2`) exist in the same graph.
49+
Starting from `n1`, if we can find `n2` through DFS, they have the same value.
50+
51+
Time: O(EV).
52+
Building the graph takes O(E). Finding contradiction takes O(EV).
53+
E is the number of edges, in this case, `len(equations)`.
54+
V is the number of nodes, in this case, the unique variables in the equations.
55+
56+
Space: O(V). For the graph.
57+
"""

0 commit comments

Comments
 (0)