leetcode53
来源:互联网 发布:什么是scratch编程 编辑:程序博客网 时间:2024/06/05 08:22
求总和最大的子数组。
动态规划dp。
Suppose we’ve solved the problem for A[1 .. i - 1]; how can we extend that to A[1 .. i]? The maximum
sum in the first I elements is either the maximum sum in the first i - 1 elements (which we’ll call MaxSoFar), or it is that of a subvector that ends in position i (which we’ll call MaxEndingHere).
MaxEndingHere is either A[i] plus the previous MaxEndingHere, or just A[i], whichever is larger.
public class Solution { public int maxSubArray(int[] nums) { int maxSoFar = nums[0], maxEndingHere = nums[0]; for(int i=1; i<nums.length; i++) { maxEndingHere = Math.max(maxEndingHere+nums[i], nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; }}
0 0
- leetcode53
- [LeetCode53]Merge Sorted Array
- LeetCode53:Maximum Subarray
- [Java]LeetCode53 Maximum Subarray
- Leetcode53 Maximum Subarray
- [leetcode53]Maximum Subarray
- LeetCode53. Maximum Subarray
- Leetcode53: Maximum Subarray
- LeetCode53. Maximum Subarray
- LeetCode53. Maximum Subarray题解
- leetcode53~Maximum Subarray
- leetcode53. Maximum Subarray
- LeetCode53 Maximum Subarray
- LeetCode53. Maximum Subarray
- LeetCode53. Maximum Subarray
- leetcode53. Maximum Subarray
- Leetcode53[easy]--Maximum Subarray
- LeetCode53——Maximum Subarray
- 第30课:彻底解密Spark 2.1.X中Shuffle中JVM内存使用及配置内幕详情
- 修改Android打包时 assets下文件拷贝
- (转)centos liveCD liveDVD netinstall minimal DVD1 DVD2 版本区别
- hdu1728 逃离迷宫 dfs搜索剪枝
- eclipse 调式 step into/step over/Drop to Frame
- leetcode53
- 删除记录遇到约束问题
- Google的Python开源Fire工具使用笔记
- 生活本就这样
- Shiro自定义Ream
- 几年来学习的曲折路线
- 运用Autoconf和Automake生产Makefile的学习之路
- LeetCode 64. Minimum Path Sum
- bzoj 4503: 两个串 (FFT+DP)