Skip to content

Commit bdfb929

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

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

data_structures/arrays/sliding_window.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@
44
https://www.geeksforgeeks.org/sum-minimum-maximum-elements-subarrays-size-k/
55
"""
66

7-
#this function will return sum of min and max of the window
8-
def get_min_max(array:list ,start:int ,end:int) -> int:
9-
7+
8+
# this function will return sum of min and max of the window
9+
def get_min_max(array: list, start: int, end: int) -> int:
1010
max = min = array[start]
11-
12-
for i in range(start+1,end+1):
11+
12+
for i in range(start + 1, end + 1):
1313
if array[i] < min:
1414
min = array[i]
1515
if array[i] > max:
1616
max = array[i]
17-
18-
return max+min
1917

20-
def sum(array:list ,size:int ,k:int) -> int:
18+
return max + min
19+
20+
21+
def sum(array: list, size: int, k: int) -> int:
2122
"""
2223
Args:
2324
array (list): the input array
2425
size (int): size of the array
2526
k (int): size of the sub-array
26-
27+
2728
Returns:
2829
int : sum of the minimum and maximum elements of all sub-arrays of size-k
2930
"""
@@ -33,19 +34,20 @@ def sum(array:list ,size:int ,k:int) -> int:
3334
18
3435
"""
3536
# create first window of size k
36-
start ,end = 0 ,k-1
37+
start, end = 0, k - 1
3738
result = 0
38-
#get the minimum and maximum element from the window and add it to the result
39-
result += get_min_max(array,size,start,end)
40-
41-
42-
while start < size-k and end < size:
39+
# get the minimum and maximum element from the window and add it to the result
40+
result += get_min_max(array, size, start, end)
41+
42+
while start < size - k and end < size:
4343
start += 1
4444
end += 1
45-
result += get_min_max(array,start,end)
46-
45+
result += get_min_max(array, start, end)
46+
4747
return result
4848

49-
if __name__ == '__main__':
49+
50+
if __name__ == "__main__":
5051
import doctest
51-
doctest.testmod()
52+
53+
doctest.testmod()

0 commit comments

Comments
 (0)