Skip to content

Commit 6885d94

Browse files
authored
Add files via upload
1 parent 85e4ef1 commit 6885d94

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def min_sub_array_len(target, nums):
2+
# Initialize window_size with positive infinity
3+
window_size = float('inf')
4+
# Initialize start and sum with 0
5+
start = 0
6+
sum = 0
7+
# Iterate over the input array
8+
for end in range(len(nums)):
9+
sum += nums[end]
10+
# Remove elements from the start of the window while sum is greater than target
11+
while sum >= target:
12+
# Find size of current window
13+
curr_subarr_size = (end + 1) - start
14+
window_size = min(window_size, curr_subarr_size)
15+
# Remove element from the start of the window
16+
sum -= nums[start]
17+
start += 1
18+
19+
if window_size != float('inf'):
20+
return window_size
21+
22+
return 0
23+
24+
25+
# Driver code
26+
def main():
27+
target = [7, 4, 11, 10, 5, 15]
28+
input_arr = [[2, 3, 1, 2, 4, 3], [1, 4, 4], [1, 1, 1, 1, 1, 1, 1, 1],
29+
[1, 2, 3, 4], [1, 2, 1, 3], [5, 4, 9, 8, 11, 3, 7, 12, 15, 44]]
30+
for i in range(len(input_arr)):
31+
window_size = min_sub_array_len(target[i], input_arr[i])
32+
print(i+1, ".\t Input array: ", input_arr[i],"\n\t Target: ", target[i],
33+
"\n\t Minimum Length of Subarray: ", window_size, sep="")
34+
print("-"*100)
35+
36+
37+
if __name__ == "__main__":
38+
main()

0 commit comments

Comments
 (0)