@@ -16,8 +16,8 @@ def __init__(self, nums: list[int], mode: str='max') -> None:
1616 """
1717 self .size = len (nums )
1818 self .mode = mode
19- if mode not in {" max" , " sum" }:
20- self .mode = " max" # Default to max if invalid mode is given
19+ if mode not in {' max' , ' sum' }:
20+ self .mode = ' max' # Default to max if invalid mode is given
2121
2222 # Build the tree from the input list
2323 self .root = self .build (0 , self .size - 1 , nums )
@@ -45,7 +45,7 @@ def build(self, start: int, end: int, nums: list[int]) -> Node:
4545 root .right = self .build (mid + 1 , end , nums )
4646
4747 # Set the value according to the mode
48- if self .mode == " max" :
48+ if self .mode == ' max' :
4949 root .value = max (root .left .value , root .right .value )
5050 else :
5151 root .value = root .left .value + root .right .value
@@ -57,11 +57,11 @@ def max_in_range(self, start_index: int, end_index: int) -> int:
5757 Queries the maximum value in a given range.
5858 Only works in 'max' mode.
5959 """
60- if self .mode == " sum" :
61- raise Exception (" Current Segment Tree doesn't support finding max" )
60+ if self .mode == ' sum' :
61+ raise Exception (' Current Segment Tree doesn\ ' t support finding max' )
6262
6363 if start_index > end_index or start_index < 0 or end_index >= self .size :
64- raise Exception (" Invalid index" )
64+ raise Exception (' Invalid index' )
6565
6666 return self .query (self .root , start_index , end_index , 0 , self .size - 1 )
6767
@@ -70,11 +70,11 @@ def sum_in_range(self, start_index: int, end_index: int) -> int:
7070 Queries the sum of values in a given range.
7171 Only works in 'sum' mode.
7272 """
73- if self .mode == " max" :
74- raise Exception (" Current Segment Tree doesn't support summing" )
73+ if self .mode == ' max' :
74+ raise Exception (' Current Segment Tree doesn\ ' t support summing' )
7575
7676 if start_index > end_index or start_index < 0 or end_index >= self .size :
77- raise Exception (" Invalid index" )
77+ raise Exception (' Invalid index' )
7878
7979 return self .query (self .root , start_index , end_index , 0 , self .size - 1 )
8080
@@ -102,7 +102,7 @@ def query(self, node: Node, start_index: int, end_index: int, start: int, end: i
102102 return self .query (node .right , start_index , end_index , mid + 1 , end )
103103 else :
104104 # Range spans both children
105- if self .mode == " max" :
105+ if self .mode == ' max' :
106106 return max (self .query (node .left , start_index , end_index , start , mid ), self .query (node .right , start_index , end_index , mid + 1 , end ))
107107 else :
108108 return self .query (node .left , start_index , end_index , start , mid ) + self .query (node .right , start_index , end_index , mid + 1 , end )
@@ -114,7 +114,7 @@ def update(self, index: int, new_value: int) -> int:
114114 :param new_value: New value to set.
115115 """
116116 if index < 0 or index >= self .size :
117- raise Exception (" Invalid index" )
117+ raise Exception (' Invalid index' )
118118
119119 self .modify (self .root , index , new_value , 0 , self .size - 1 )
120120
@@ -139,7 +139,7 @@ def modify(self, node: Node, index: int, new_value: int, start: int, end: int) -
139139 self .modify (node .right , index , new_value , mid + 1 , end )
140140
141141 # Recompute current node's value after update
142- if self .mode == " max" :
142+ if self .mode == ' max' :
143143 node .value = max (node .left .value , node .right .value )
144144 else :
145145 node .value = node .left .value + node .right .value
0 commit comments