Skip to content

Commit 28d2fcc

Browse files
committed
maximum subarray solution
1 parent e587e27 commit 28d2fcc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

maximum-subarray/dylan-jung.cpp

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

0 commit comments

Comments
 (0)