53. Maximum Subarray
2016-01-12 22:23
274 查看
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
动态规划。
对于数组nums,以nums[i]结尾的子数组最大值为
max[i] = nums[i]+Math.max(max[i-1],0)
由于当前max[i]仅和max[i-1]有关,可以简化成一个变量
提示说:If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
总体思想:left middle right 之间的最大子数组为left~middle,middle+1~right,和middle向两侧扩展的子数组三个的最大值。
For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.
动态规划。
对于数组nums,以nums[i]结尾的子数组最大值为
max[i] = nums[i]+Math.max(max[i-1],0)
由于当前max[i]仅和max[i-1]有关,可以简化成一个变量
public int maxSubArray(int[] nums) { int l = nums.length; if (l == 0) return 0; if (l == 1) return nums[0]; int last = nums[0]; //以nums[i]结尾的子数组最大值 int max = last; //最大子数组最大值 for (int i = 1; i < l; i++) { if (last <= 0 ) { last = nums[i]; } else { last = last +nums[i]; } max = Math.max(max, last); } return max; }
提示说:If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
总体思想:left middle right 之间的最大子数组为left~middle,middle+1~right,和middle向两侧扩展的子数组三个的最大值。
public int maxSubArray(int[] nums) { int l = nums.length; if (l == 0) return 0; if (l == 1) return nums[0]; return helper(nums,0,l-1); } private int helper(int[] nums,int left,int right){ if(left == right) return nums[left]; int middle = (right - left)/2+left; int leftpart = helper(nums,left,middle); //左半边最大值 int rightpart = helper(nums,middle+1,right); //右半边最大值 int leftmax = nums[middle],rightmax = nums[middle+1]; //从middle向两侧扩展的最大值 int tmp = 0; for(int i = middle;i >= left;i--){ tmp += nums[i]; leftmax = Math.max(tmp,leftmax); } tmp = 0; for(int i = middle+1;i <= right;i++){ tmp += nums[i]; rightmax = Math.max(tmp,rightmax); } return Math.max(Math.max(leftpart,rightpart),rightmax+leftmax); }
相关文章推荐
- SRAtoolkit使用
- 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11
- 浅谈js怎么实现全选按钮
- Errors occurred during the build.
- nodejs Tips7
- 大叔也说Xamarin~Android篇~为HttpClient共享Session,android与api的session共享机制
- 数据段描述符和代码段描述符(二)——《x86汇编语言:从实模式到保护模式》读书笔记11
- [JAVA修炼之路四]-JVM内存模型以及对象实例化过程
- iOS 记录错误信息(附 源码)
- Hdaoop环境配置及中文分词加排序
- 使用URLConnection进行http请求
- IP/TCP 端口
- C和Java效率对比试验和编译器优化影响
- lock()
- "产品测试管理&敏捷项目管理"研讨会在深圳成功举办!
- java集合总结
- iOS后台模式
- python学习第二天
- Android网络请求
- batch.bat explaination