Skip to content

Commit ba6d06f

Browse files
authored
Merge pull request #511 from thevijayshankersharma/0053-solution
Add a Solution for Maximum Subarray (LeetCode Problem 53)
2 parents 730bb6f + 5696426 commit ba6d06f

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
id: maximum-subarray
3+
title: Maximum Subarray (LeetCode)
4+
difficulty: Medium
5+
sidebar_label: 0053-MaximumSubarray
6+
topics:
7+
- Array
8+
- Divide and Conquer
9+
companies:
10+
- ""
11+
description: |
12+
Given an integer array nums, find the subarray with the largest sum, and return its sum.
13+
---
14+
15+
## Problem Description
16+
17+
| Problem Statement | Solution Link | LeetCode Profile |
18+
| :---------------- | :------------ | :--------------- |
19+
| [Merge Two Sorted Lists](https://leetcode.com/problems/maximum-subarray/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/maximum-subarray/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
20+
21+
## Problem Description
22+
23+
Given an integer array `nums`, find the subarray with the largest sum, and return its sum.
24+
25+
### Examples
26+
27+
#### Example 1
28+
29+
- **Input:** nums = [-2,1,-3,4,-1,2,1,-5,4]
30+
- **Output:** 6
31+
- **Explanation:** The subarray [4,-1,2,1] has the largest sum 6.
32+
33+
#### Example 2
34+
35+
- **Input:** nums = [1]
36+
- **Output:** 1
37+
- **Explanation:** The subarray [1] has the largest sum 1.
38+
39+
#### Example 3
40+
41+
- **Input:** nums = [5,4,-1,7,8]
42+
- **Output:** 23
43+
- **Explanation:** The subarray [5,4,-1,7,8] has the largest sum 23.
44+
45+
### Constraints
46+
47+
- `1 <= nums.length <= 10^5`
48+
- `-10^4 <= nums[i] <= 10^4`
49+
50+
### Approach
51+
52+
To find the subarray with the largest sum, we can utilize the Kadane's algorithm, which efficiently solves this problem in linear time complexity O(n).
53+
54+
#### Kadane's Algorithm
55+
56+
1. Initialize two variables `max_sum` and `current_sum` to store the maximum sum found so far and the current sum of subarray, respectively.
57+
2. Iterate through the array:
58+
- Update `current_sum` by adding the current element to it.
59+
- If `current_sum` becomes negative, reset it to 0 (indicating the start of a new potential subarray).
60+
- Update `max_sum` if `current_sum` is greater than `max_sum`.
61+
3. Finally, return `max_sum`.
62+
63+
### Solution Code
64+
65+
#### Python
66+
67+
```
68+
class Solution(object):
69+
def maxSubArray(self, nums):
70+
if not nums:
71+
return 0
72+
max_sum = curr_sum = nums[0]
73+
for num in nums[1:]:
74+
curr_sum = max(num, curr_sum + num)
75+
max_sum = max(max_sum, curr_sum)
76+
return max_sum
77+
```
78+
79+
#### C++
80+
81+
```
82+
class Solution {
83+
public:
84+
int maxSubArray(vector<int>& nums) {
85+
int max_sum = nums[0];
86+
int current_sum = 0;
87+
for (int num : nums) {
88+
current_sum += num;
89+
max_sum = max(max_sum, current_sum);
90+
if (current_sum < 0)
91+
current_sum = 0;
92+
}
93+
return max_sum;
94+
}
95+
};
96+
```
97+
98+
#### Java
99+
100+
```
101+
class Solution {
102+
public int maxSubArray(int[] nums) {
103+
int max_sum = nums[0];
104+
int current_sum = 0;
105+
for (int num : nums) {
106+
current_sum += num;
107+
max_sum = Math.max(max_sum, current_sum);
108+
if (current_sum < 0)
109+
current_sum = 0;
110+
}
111+
return max_sum;
112+
}
113+
}
114+
```
115+
116+
### Conclusion
117+
118+
The Maximum Subarray problem can be efficiently solved using Kadane's algorithm, which finds the subarray with the largest sum in linear time complexity O(n). The provided solution code implements this algorithm in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)