53.|53. Maximum Subarray
https://leetcode.com/problems/maximum-subarray/description/
解题思路:
- 首先确定其是动态规划(最有子结构,重叠子问题,无后效性)
- 比较当前index的值和当前index值+index-1的sum值,其最大值为当前index的sum值,最后比较所有sum值得到最大子数组的值
class Solution {
public int maxSubArray(int[] nums) {
int len = nums.length;
if(len == 1) return nums[0];
int[] dp = new int[len];
dp[0] = nums[0];
int max = dp[0];
for(int i = 1;
i < len;
i++){
dp[i] = Math.max(nums[i], nums[i] + dp[i - 1]);
max = Math.max(dp[i], max);
}
return max;
}
}
推荐阅读
- Maximum|Maximum Subsequence Sum
- Maximum execution time of 30 seconds exceeded解决办法
- LeetCode|LeetCode 53. 最大子序和(动态规划)
- [codeforces 1365E] Maximum Subsequence Value 或运算+为什么选三个元素对应最大值
- MaximumGap的思想
- [cf]|[cf] 1401 D.Maximum Distributed Tree
- DEDE 织梦cms出现Fatal error: Maximum execution time of 30 seconds exceeded in解决办法
- Maximum|Maximum execution time of 30 seconds exceeded
- 处理PHP|处理PHP mysql_query执行超时 【Fatal error: Maximum execution time of 300 seconds exceeded in……】...
- PHP中超时提示Fatal error: Maximum execution time of 30 seconds exceeded的解决方案