Skip to content

Commit

Permalink
Merge pull request #228 from imbeshat/53_Maximum_Subarray
Browse files Browse the repository at this point in the history
Maximum Subarray DSA problem Leetcode
  • Loading branch information
iamdestinychild authored Oct 10, 2023
2 parents f608730 + 54c37a0 commit 42f14d8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LeetCode/53-Maximum-Subarray/C++/53-maximum-subarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int maxSum = INT_MIN;
int curSum = 0;
for(int i=0;i<nums.size();i++){
curSum+=nums[i];
if(curSum>maxSum){
maxSum=curSum;
}
if(curSum<0){
curSum=0;
}
}
return maxSum;
}
};
35 changes: 35 additions & 0 deletions LeetCode/53-Maximum-Subarray/C++/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h2><a href="https://leetcode.com/problems/maximum-subarray/">53. Maximum Subarray</a></h2><h3>Medium</h3><hr><div><p>Given an integer array <code>nums</code>, find the contiguous subarray (containing at least one number) which has the largest sum and return <em>its sum</em>.</p>

<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre><strong>Input:</strong> nums = [-2,1,-3,4,-1,2,1,-5,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> [4,-1,2,1] has the largest sum = 6.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre><strong>Input:</strong> nums = [1]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre><strong>Input:</strong> nums = [5,4,-1,7,8]
<strong>Output:</strong> 23
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> If you have figured out the <code>O(n)</code> solution, try coding another solution using the <strong>divide and conquer</strong> approach, which is more subtle.</p>
</div>
Binary file added LeetCode/53-Maximum-Subarray/C++/Screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 42f14d8

Please sign in to comment.