Skip to content

Commit 25d453e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent e8cd5c1 commit 25d453e

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

data_structures/arrays/sparse_table.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Finding the minimum value of a subset [L..R] of a static array.
77
88
Overall time complexity: O(nlogn)
9-
Overall space complexity: O(nlogn)
9+
Overall space complexity: O(nlogn)
1010
1111
Wikipedia link: https://en.wikipedia.org/wiki/Range_minimum_query
1212
"""
@@ -35,13 +35,12 @@ def build_sparse_table(arr: list[int], n: int) -> list[int]:
3535
k = int(math.log2(n)) + 1
3636
lookup = [[0 for i in range(n)] for j in range(k)]
3737

38-
for i in range(0, n):
39-
lookup[0][i] = arr[i]
38+
for i in range(0, n):
39+
lookup[0][i] = arr[i]
4040

4141
j = 1
4242

43-
while (1 << j) <= n:
44-
43+
while (1 << j) <= n:
4544
# Compute the minimum value for all intervals with size (2 ** j)
4645
i = 0
4746
while (i + (1 << j) - 1) < n:
@@ -82,12 +81,14 @@ def query(lookup: list[int], L: int, R: int) -> int:
8281
raise IndexError("list index out of range")
8382

8483
"""
85-
Find the highest power of 2
84+
Find the highest power of 2
8685
that is at least the number of elements in a given range.
8786
"""
88-
j = int(math.log2(R - L + 1))
89-
return min(lookup[j][R - (1 << j) + 1], lookup[j][L])
90-
87+
j = int(math.log2(R - L + 1))
88+
return min(lookup[j][R - (1 << j) + 1], lookup[j][L])
89+
90+
9191
if __name__ == "__main__":
9292
from doctest import testmod
93-
testmod()
93+
94+
testmod()

0 commit comments

Comments
 (0)