1010
1111 Wikipedia link: https://en.wikipedia.org/wiki/Range_minimum_query
1212"""
13+ from math import log2
1314
1415
1516def build_sparse_table (number_list : list [int ]) -> list [list [int ]]:
@@ -26,27 +27,20 @@ def build_sparse_table(number_list: list[int]) -> list[list[int]]:
2627 ...
2728 ValueError: empty number list not allowed
2829 """
29- import math
30-
31- if number_list == []:
30+ if not number_list :
3231 raise ValueError ("empty number list not allowed" )
3332
3433 length = len (number_list )
35- """
36- Initialise sparse_table
37-
38- sparse_table[j][i] represents the minimum value of
39- the subset of length (2 ** j) of number_list, starting from index i.
40- """
34+ # Initialise sparse_table -- sparse_table[j][i] represents the minimum value of the
35+ # subset of length (2 ** j) of number_list, starting from index i.
4136
4237 # smallest power of 2 subset length that fully covers number_list
43- row = int (math . log2 (length )) + 1
38+ row = int (log2 (length )) + 1
4439 sparse_table = [[0 for i in range (length )] for j in range (row )]
4540
4641 # minimum of subset of length 1 is that value itself
4742 for i , value in enumerate (number_list ):
4843 sparse_table [0 ][i ] = value
49-
5044 j = 1
5145
5246 # compute the minimum value for all intervals with size (2 ** j)
@@ -58,11 +52,8 @@ def build_sparse_table(number_list: list[int]) -> list[list[int]]:
5852 sparse_table [j ][i ] = min (
5953 sparse_table [j - 1 ][i + (1 << (j - 1 ))], sparse_table [j - 1 ][i ]
6054 )
61-
6255 i += 1
63-
6456 j += 1
65-
6657 return sparse_table
6758
6859
@@ -72,37 +63,32 @@ def query(sparse_table: list[list[int]], left_bound: int, right_bound: int) -> i
7263 0
7364 >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 4, 6)
7465 3
75- >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 11)
76- Traceback (most recent call last):
77- ...
78- IndexError: list index out of range
79-
8066 >>> query(build_sparse_table([3, 1, 9]), 2, 2)
8167 9
8268 >>> query(build_sparse_table([3, 1, 9]), 0, 1)
8369 1
84-
70+ >>> query(build_sparse_table([8, 1, 0, 3, 4, 9, 3]), 0, 11)
71+ Traceback (most recent call last):
72+ ...
73+ IndexError: list index out of range
8574 >>> query(build_sparse_table([]), 0, 0)
8675 Traceback (most recent call last):
8776 ...
8877 ValueError: empty number list not allowed
8978 """
90- import math
91-
9279 if left_bound < 0 or right_bound >= len (sparse_table [0 ]):
9380 raise IndexError ("list index out of range" )
9481
9582 # highest subset length of power of 2 that is within range [left_bound, right_bound]
96- j = int (math . log2 (right_bound - left_bound + 1 ))
83+ j = int (log2 (right_bound - left_bound + 1 ))
9784
98- """
99- minimum of 2 overlapping smaller subsets: [left_bound, left_bound + 2 ** j - 1]
100- and [right_bound - 2 ** j + 1, right_bound]
101- """
85+ # minimum of 2 overlapping smaller subsets:
86+ # [left_bound, left_bound + 2 ** j - 1] and [right_bound - 2 ** j + 1, right_bound]
10287 return min (sparse_table [j ][right_bound - (1 << j ) + 1 ], sparse_table [j ][left_bound ])
10388
10489
10590if __name__ == "__main__" :
10691 from doctest import testmod
10792
10893 testmod ()
94+ print (f"{ query (build_sparse_table ([3 , 1 , 9 ]), 2 , 2 ) = } " )
0 commit comments