Skip to content

Commit 98dc118

Browse files
committed
Runtime: 5273 ms (Top 38.81%) | Memory: 47.1 MB (Top 89.55%)
1 parent f374a12 commit 98dc118

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

scripts/algorithms/L/Longest Common Subpath/Longest Common Subpath.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Runtime: 5273 ms (Top 38.81%) | Memory: 47.1 MB (Top 89.55%)
2+
13
class Solution:
24
def longestCommonSubpath(self, n, paths) -> int:
35
def get_common_subpath_hashes(k):
@@ -10,10 +12,10 @@ def get_subpath_hashes(path):
1012
else:
1113
yield hash
1214
if i < len(path):
13-
hash = ((hash-coeff*path[i-k])*n + path[i]) % mod
15+
hash = ((hash-coeff*path[i-k])*n + path[i]) % mod
1416
return reduce(set.intersection, (set(get_subpath_hashes(p)) for p in paths))
15-
16-
# can be replaced with a pre-computed large prime
17+
18+
# can be replaced with a pre-computed large prime
1719
mod = self._generate_large_prime(int(1e18), int(9e18))
1820
low, high = 1, min(len(p) for p in paths)+1
1921
while low < high:
@@ -23,7 +25,7 @@ def get_subpath_hashes(path):
2325
else:
2426
high = mid
2527
return high - 1
26-
28+
2729
def _generate_large_prime(self, lower, upper):
2830
"""Generate a prime between [lower, upper)"""
2931
def is_prime(n, trials=50):
@@ -40,4 +42,4 @@ def witness(a, n):
4042
while u%2 == 0:
4143
t, u = t+1, u>>1
4244
return not any(witness(randrange(1, n), n) for _ in range(trials))
43-
return next(r for r in iter(lambda: randrange(lower, upper), None) if is_prime(r))
45+
return next(r for r in iter(lambda: randrange(lower, upper), None) if is_prime(r))

0 commit comments

Comments
 (0)