-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSatisfiability of Equality Equations.py
72 lines (55 loc) · 1.71 KB
/
Satisfiability of Equality Equations.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
62
63
64
65
66
67
68
69
70
71
72
from random import randint
from string import ascii_lowercase
from typing import Iterable, List
class DSU:
"""
Disjoint set system with rank and path compression heuristics
Memory:
creation - O(n)
'find' - O(log(n))
'union' - O(log(n))
Time:
creation - O(n)
'find' - O(log(n))
'union' - O(log(n))
On average, operations 'find' and 'union' take O(1) time and space
"""
def __init__(self, nodes: Iterable):
self.parent = {n: n for n in nodes}
self.rank = {n: 1 for n in nodes}
def find(self, a: int) -> int:
if a != self.parent[a]:
self.parent[a] = self.find(self.parent[a])
return self.parent[a]
def union(self, a: int, b: int):
a_root = self.find(a)
b_root = self.find(b)
if a_root == b_root:
return
if self.rank[a_root] > self.rank[b_root]:
self.parent[b_root] = a_root
elif self.rank[a_root] < self.rank[b_root]:
self.parent[a_root] = b_root
else:
if randint(0, 1) & 1:
a_root, b_root = b_root, a_root
self.parent[b_root] = a_root
self.rank[a_root] += 1
class Solution:
"""
Time: O(n)
Memory: O(n)
"""
EQUAL = '='
NOT_EQUAL = '!'
def equationsPossible(self, equations: List[str]) -> bool:
dsu = DSU(ascii_lowercase)
for eq in equations:
i, sign, _, j = list(eq)
if sign == self.EQUAL:
dsu.union(i, j)
for eq in equations:
i, sign, _, j = list(eq)
if sign == self.NOT_EQUAL and dsu.find(i) == dsu.find(j):
return False
return True