Created
October 10, 2017 16:22
-
-
Save cixuuz/9f2f84743844cb042ffa2b322f802d80 to your computer and use it in GitHub Desktop.
[53. Maximum Subarray] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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