-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathequal-row-and-column-pairs.py
More file actions
77 lines (70 loc) · 2.39 KB
/
equal-row-and-column-pairs.py
File metadata and controls
77 lines (70 loc) · 2.39 KB
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
73
74
75
76
77
# 2352. Equal Row and Column Pairs
# 🟠 Medium
#
# https://leetcode.com/problems/equal-row-and-column-pairs/
#
# Tags: Array - Hash Table - Matrix - Simulation
import timeit
from typing import List
# Use a Trie-like structure, keys are the values in the cells, the mark
# for end-of-word also contains the number of columns that we have
# seen have the exact same sequence of values. First process rows or
# columns inserting all value sequences found, then iterate over the
# other checking for matches.
#
# Time complexity: O(n^2) - Where n is the row/column length, we visit
# twice each value in the grid.
# Space complexity: O(n^2) - We can have one entry in the trie per each
# value in the grid.
#
# Runtime 537 ms Beats 52.11%
# Memory 27 MB Beats 5.10%
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
root = {}
for row in grid:
current = root
for num in row:
if num not in current:
current[num] = {}
current = current[num]
if "count" in current:
current["count"] += 1
else:
current["count"] = 1
res = 0
# Iterate over the columns.
for c in range(len(grid[0])):
current = root
for r in range(len(grid)):
num = grid[r][c]
if num not in current:
break
current = current[num]
else:
if "count" in current:
res += current["count"]
return res
def test():
executors = [Solution]
tests = [
[[[3, 2, 1], [1, 7, 6], [2, 7, 7]], 1],
[[[3, 1, 2, 2], [1, 4, 4, 5], [2, 4, 2, 2], [2, 4, 2, 2]], 3],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
sol = executor()
result = sol.equalPairs(t[0])
exp = t[1]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()