Created
September 7, 2023 20:12
-
-
Save athursto/02bd5c242a146abee489492b290b2ca5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""9.7.2023""" | |
"""Given an array of integers and a number k (where k is guaranteed to be less than the array's length), return a | |
subarray of length k with the minimum possible sum. Maintain the order of the original array!""" | |
def min_array(array, k): | |
pointer, endpoint, min_sum, ideal_array = 0, k, 1000000000, None | |
while pointer <= (len(array) - k): | |
sub_array = array[pointer: endpoint] | |
print(f"assessing {sub_array}") | |
if sum(sub_array) < min_sum: | |
print(f"sub_array {sub_array} has a min_sum of {sum(sub_array)}, changing min_sum") | |
ideal_array = sub_array | |
min_sum = sum(sub_array) | |
pointer += 1 | |
endpoint += 1 | |
return ideal_array | |
test_array = [1,3,20,4,8,9,11] | |
length = 3 | |
print(min_array(test_array, length)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment