Skip to content

Commit 73815d8

Browse files
author
Hamid Gasmi
committed
Issue #32 1st draft is added
1 parent d96e4a9 commit 73815d8

File tree

3 files changed

+200040
-0
lines changed

3 files changed

+200040
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Database:
2+
def __init__(self, row_counts):
3+
self.row_counts = row_counts
4+
self.max_row_count = max(row_counts)
5+
n_tables = len(row_counts)
6+
self.ranks = [1] * n_tables
7+
self.parents = list(range(n_tables))
8+
9+
def merge(self, src, dst):
10+
src_parent = self.get_parent(src)
11+
dst_parent = self.get_parent(dst)
12+
13+
if src_parent == dst_parent:
14+
return False
15+
16+
# merge two components
17+
# use union by rank heuristic
18+
# update max_row_count with the new maximum table size
19+
return True
20+
21+
def get_parent(self, table):
22+
# find parent and compress path
23+
return self.parents[table]
24+
25+
26+
def main():
27+
n_tables, n_queries = map(int, input().split())
28+
counts = list(map(int, input().split()))
29+
assert len(counts) == n_tables
30+
db = Database(counts)
31+
for i in range(n_queries):
32+
dst, src = map(int, input().split())
33+
db.merge(dst - 1, src - 1)
34+
print(db.max_row_count)
35+
36+
37+
if __name__ == "__main__":
38+
main()

2-data-sructures-fundamentals/3_priority_queues_and_disjoint_sets/merging_tables_tests/116

+100,002
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)