Skip to content

Commit 2fce508

Browse files
committed
maximum subarray solution
1 parent bfadb7c commit 2fce508

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

maximum-subarray/PDKhan.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int cur = nums[0];
5+
int max = nums[0];
6+
7+
for(int i = 1; i < nums.size(); i++){
8+
if(nums[i] > cur + nums[i])
9+
cur = nums[i];
10+
else
11+
cur = cur + nums[i];
12+
13+
if(max < cur)
14+
max = cur;
15+
}
16+
17+
return max;
18+
}
19+
};

0 commit comments

Comments
 (0)