Skip to content

Commit eb14adf

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 53_Maximum_Subarray.java
1 parent 95d1435 commit eb14adf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
if (nums == null || nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int[] dp = new int[nums.length];
8+
int max = dp[0] = nums[0];
9+
10+
for (int i = 1; i < nums.length; i++) {
11+
dp[i] = Math.max(nums[i], nums[i] + dp[i - 1]);
12+
max = Math.max(max, dp[i]);
13+
}
14+
15+
return max;
16+
}
17+
}

0 commit comments

Comments
 (0)