File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # max_sliding_window_sum.py
2+
3+ # Input list
4+ n = [5 , 9 , 1 , 8 , 7 ]
5+
6+ # Running sum of current window
7+ temp = 0
8+
9+ # Left pointer of sliding window
10+ l = 0
11+
12+ # Variable to store the maximum sum of any window
13+ ans = 0
14+
15+ # Iterate through the list
16+ for i in range (0 , len (n )):
17+ # Add the current element to running sum
18+ temp += n [i ]
19+
20+ # If window size exceeds 3, remove the leftmost element
21+ if (i - l == 3 ):
22+ temp -= n [l ] # remove element going out of window
23+ l += 1 # move left pointer
24+ print (l , i ) # debug: shows window boundaries
25+
26+ # If window size is exactly 3, check window sum
27+ if (i - l + 1 == 3 ):
28+ print (temp ) # print the current window sum
29+ ans = max (ans ,temp ) # update maximum window sum
30+
31+ # Print the maximum sum found
32+ print (ans )
You can’t perform that action at this time.
0 commit comments