@@ -16,8 +16,8 @@ def __init__(self, nums: list[int], mode: str='max') -> None:
16
16
"""
17
17
self .size = len (nums )
18
18
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
21
21
22
22
# Build the tree from the input list
23
23
self .root = self .build (0 , self .size - 1 , nums )
@@ -45,7 +45,7 @@ def build(self, start: int, end: int, nums: list[int]) -> Node:
45
45
root .right = self .build (mid + 1 , end , nums )
46
46
47
47
# Set the value according to the mode
48
- if self .mode == " max" :
48
+ if self .mode == ' max' :
49
49
root .value = max (root .left .value , root .right .value )
50
50
else :
51
51
root .value = root .left .value + root .right .value
@@ -57,11 +57,11 @@ def max_in_range(self, start_index: int, end_index: int) -> int:
57
57
Queries the maximum value in a given range.
58
58
Only works in 'max' mode.
59
59
"""
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' )
62
62
63
63
if start_index > end_index or start_index < 0 or end_index >= self .size :
64
- raise Exception (" Invalid index" )
64
+ raise Exception (' Invalid index' )
65
65
66
66
return self .query (self .root , start_index , end_index , 0 , self .size - 1 )
67
67
@@ -70,11 +70,11 @@ def sum_in_range(self, start_index: int, end_index: int) -> int:
70
70
Queries the sum of values in a given range.
71
71
Only works in 'sum' mode.
72
72
"""
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' )
75
75
76
76
if start_index > end_index or start_index < 0 or end_index >= self .size :
77
- raise Exception (" Invalid index" )
77
+ raise Exception (' Invalid index' )
78
78
79
79
return self .query (self .root , start_index , end_index , 0 , self .size - 1 )
80
80
@@ -102,7 +102,7 @@ def query(self, node: Node, start_index: int, end_index: int, start: int, end: i
102
102
return self .query (node .right , start_index , end_index , mid + 1 , end )
103
103
else :
104
104
# Range spans both children
105
- if self .mode == " max" :
105
+ if self .mode == ' max' :
106
106
return max (self .query (node .left , start_index , end_index , start , mid ), self .query (node .right , start_index , end_index , mid + 1 , end ))
107
107
else :
108
108
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:
114
114
:param new_value: New value to set.
115
115
"""
116
116
if index < 0 or index >= self .size :
117
- raise Exception (" Invalid index" )
117
+ raise Exception (' Invalid index' )
118
118
119
119
self .modify (self .root , index , new_value , 0 , self .size - 1 )
120
120
@@ -139,7 +139,7 @@ def modify(self, node: Node, index: int, new_value: int, start: int, end: int) -
139
139
self .modify (node .right , index , new_value , mid + 1 , end )
140
140
141
141
# Recompute current node's value after update
142
- if self .mode == " max" :
142
+ if self .mode == ' max' :
143
143
node .value = max (node .left .value , node .right .value )
144
144
else :
145
145
node .value = node .left .value + node .right .value
0 commit comments