Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 10, 2017 16:22
Show Gist options
  • Save cixuuz/9f2f84743844cb042ffa2b322f802d80 to your computer and use it in GitHub Desktop.
Save cixuuz/9f2f84743844cb042ffa2b322f802d80 to your computer and use it in GitHub Desktop.
[53. Maximum Subarray] #leetcode
class Solution {
public int maxSubArray(int[] nums) {
int res = 0;
if (nums == null || nums.length == 0) return 0;
int[] dp = new int[nums.length];
dp[0] = nums[0];
res = dp[0];
for (int i = 1; i < nums.length; i++) {
dp[i] = (dp[i-1] > 0? dp[i-1] : 0) + nums[i];
res = Math.max(res, dp[i]);
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment