Skip to content

Commit 3cab43b

Browse files
committed
feat(algorithms, sliding-window, max sum subarray): max sum subarray
1 parent 736636e commit 3cab43b

20 files changed

Lines changed: 142 additions & 0 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Maximum Sum of Subarray with Size K
2+
3+
Given an array of integers nums and an integer k, find the maximum sum of any contiguous subarray of size k.
4+
5+
## Examples
6+
7+
Example 1
8+
9+
```text
10+
Input: nums = [2, 1, 5, 1, 3, 2], k = 3
11+
Output: 9
12+
Explanation: The subarray with the maximum sum is [5, 1, 3] with a sum of 9.
13+
```
14+
15+
## Solution
16+
17+
We start by extending the window to size k. Whenever our window is of size k, we first compute the sum of the window and
18+
update max_sum if it is larger than max_sum. Then, we contract the window by removing the leftmost element to prepare for
19+
the next iteration. Note how we calculate the sum of the window incrementally by adding the new element and removing from
20+
the previous sum.
21+
22+
![Solution 1](./images/solutions/max_sum_of_subarray_size_k_solution_1.png)
23+
![Solution 2](./images/solutions/max_sum_of_subarray_size_k_solution_2.png)
24+
![Solution 3](./images/solutions/max_sum_of_subarray_size_k_solution_3.png)
25+
![Solution 4](./images/solutions/max_sum_of_subarray_size_k_solution_4.png)
26+
![Solution 5](./images/solutions/max_sum_of_subarray_size_k_solution_5.png)
27+
![Solution 6](./images/solutions/max_sum_of_subarray_size_k_solution_6.png)
28+
![Solution 7](./images/solutions/max_sum_of_subarray_size_k_solution_7.png)
29+
![Solution 8](./images/solutions/max_sum_of_subarray_size_k_solution_8.png)
30+
![Solution 9](./images/solutions/max_sum_of_subarray_size_k_solution_9.png)
31+
![Solution 10](./images/solutions/max_sum_of_subarray_size_k_solution_10.png)
32+
![Solution 11](./images/solutions/max_sum_of_subarray_size_k_solution_11.png)
33+
![Solution 12](./images/solutions/max_sum_of_subarray_size_k_solution_12.png)
34+
![Solution 13](./images/solutions/max_sum_of_subarray_size_k_solution_13.png)
35+
![Solution 14](./images/solutions/max_sum_of_subarray_size_k_solution_14.png)
36+
![Solution 15](./images/solutions/max_sum_of_subarray_size_k_solution_15.png)
37+
![Solution 16](./images/solutions/max_sum_of_subarray_size_k_solution_16.png)
38+
![Solution 17](./images/solutions/max_sum_of_subarray_size_k_solution_17.png)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from typing import List
2+
3+
4+
def max_sum_subarray(nums: List[int], k: int) -> int:
5+
n = len(nums)
6+
7+
# If the length of the numbers is less than k, we return 0, as we can't get a contiguous subarray of size k from nums
8+
if n < k:
9+
return 0
10+
11+
start = 0
12+
state = 0
13+
max_sum = 0
14+
15+
for end in range(n):
16+
state += nums[end]
17+
18+
if end - start + 1 == k:
19+
max_sum = max(max_sum, state)
20+
state -= nums[start]
21+
start += 1
22+
23+
return max_sum
24+
25+
def max_sum_subarray_2(nums: List[int], k: int) -> int:
26+
n = len(nums)
27+
28+
# If the length of the numbers is less than k, we return 0, as we can't get a contiguous subarray of size k from nums
29+
if n < k:
30+
return 0
31+
32+
window_sum = sum(nums[:k])
33+
max_sum = window_sum
34+
start = 0
35+
36+
for end in range(k, n):
37+
window_sum += nums[end] - nums[start]
38+
start += 1
39+
max_sum = max(max_sum, window_sum)
40+
41+
return max_sum
42+
43+
44+
def max_sum_subarray_3(nums: List[int], k: int) -> int:
45+
n = len(nums)
46+
47+
# If the length of the numbers is less than k, we return 0, as we can't get a contiguous subarray of size k from nums
48+
if n < k:
49+
return 0
50+
51+
# Use two points which will act as the boundary of the window. In this window, the sum of the elements will be
52+
# checked to get the max sum of the sub array
53+
lower_bound = 0
54+
upper_bound = k
55+
max_sum = 0
56+
57+
while upper_bound <= n:
58+
current_sum = sum(nums[lower_bound:upper_bound])
59+
max_sum = max(current_sum, max_sum)
60+
lower_bound += 1
61+
upper_bound += 1
62+
63+
return max_sum
31.7 KB
Loading
39.2 KB
Loading
39.2 KB
Loading
41.9 KB
Loading
37.8 KB
Loading
40.3 KB
Loading
39.7 KB
Loading
40.7 KB
Loading

0 commit comments

Comments
 (0)