Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions data_structures/arrays/sliding_window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Given an array of both positive and negative integers, the task is to compute sum of minimum and maximum elements of all sub-array of size k.

Check failure on line 2 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

data_structures/arrays/sliding_window.py:2:89: E501 Line too long (141 > 88)

https://www.geeksforgeeks.org/sum-minimum-maximum-elements-subarrays-size-k/
"""


# this function will return sum of min and max of the window
def get_min_max(array: list, start: int, end: int) -> int:
max = min = array[start]

Check failure on line 10 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

data_structures/arrays/sliding_window.py:10:5: A001 Variable `max` is shadowing a Python builtin

Check failure on line 10 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

data_structures/arrays/sliding_window.py:10:11: A001 Variable `min` is shadowing a Python builtin

for i in range(start + 1, end + 1):
if array[i] < min:
min = array[i]

Check failure on line 14 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/sliding_window.py:13:9: PLR1730 Replace `if` statement with `min = min(array[i], min)`

Check failure on line 14 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

data_structures/arrays/sliding_window.py:14:13: A001 Variable `min` is shadowing a Python builtin
if array[i] > max:
max = array[i]

Check failure on line 16 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

data_structures/arrays/sliding_window.py:15:9: PLR1730 Replace `if` statement with `max = max(array[i], max)`

Check failure on line 16 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

data_structures/arrays/sliding_window.py:16:13: A001 Variable `max` is shadowing a Python builtin

return max + min


def sum(array: list, size: int, k: int) -> int:

Check failure on line 21 in data_structures/arrays/sliding_window.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (A001)

data_structures/arrays/sliding_window.py:21:5: A001 Variable `sum` is shadowing a Python builtin
"""
Args:
array (list): the input array
size (int): size of the array
k (int): size of the sub-array

Returns:
int : sum of the minimum and maximum elements of all sub-arrays of size-k
"""
"""
Examples:
>>> sum([2, 5, -1, 7, -3, -1, -2], 7 ,4)
18
"""
# create first window of size k
start, end = 0, k - 1
result = 0
# get the minimum and maximum element from the window and add it to the result
result += get_min_max(array, size, start, end)

while start < size - k and end < size:
start += 1
end += 1
result += get_min_max(array, start, end)

return result


if __name__ == "__main__":
import doctest

doctest.testmod()
Loading