【LEETCODE】53-Maximum Subarray
2015-12-10 16:04
155 查看
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.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
参考: http://www.cnblogs.com/zuoyuan/p/3781988.html
分治法:
下次有时间再仔细学习
参考:
http://www.algoqueue.com/algoqueue/default/view/10354688/find-maximum-sum-subarray-divide-and-conquer-
Divide the array into two parts.
Find maximum subarray sum for left half recursively.
Find maximum subarray sum for right half recursively.
Find maximum subarray sum for the subarray including the middle element. (Sum of last two steps)
Return the maximum of the last three results.
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.
click to show more practice.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
参考: http://www.cnblogs.com/zuoyuan/p/3781988.html
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ tmpsum=0 maxsum=-100000 for i in range(0,len(nums)): if tmpsum<0: tmpsum=0 tmpsum+=nums[i] maxsum=max(tmpsum,maxsum) return maxsum
分治法:
下次有时间再仔细学习
参考:
http://www.algoqueue.com/algoqueue/default/view/10354688/find-maximum-sum-subarray-divide-and-conquer-
Divide the array into two parts.
Find maximum subarray sum for left half recursively.
Find maximum subarray sum for right half recursively.
Find maximum subarray sum for the subarray including the middle element. (Sum of last two steps)
Return the maximum of the last three results.
public static int FindMaxSumSubArray(int[] array, int low, int high){ /* No element in the array */ if (low > high) return 0; /* One element in the array */ if (low == high) return max(0, array[low]); /* Middle element of the array */ int middle = (low + high) / 2; /* find maximum sum crossing to left */ leftMax = sum = 0; for (i = middle; i ≥ low; i--) { sum += array[i]; if (sum > leftMax) leftMax = sum; } /* find maximum sum crossing to right */ rightMax = sum = 0; for (i = middle+1; i ≤ high; i++) { sum += array[i]; if (sum > rightMax) rightMax = sum; } /* Return the maximum of leftMax, rightMax and their sum */ return Math.max(leftMax + rightMax, Math.max(FindMaxSumSubArray(low, middle), FindMaxSumSubArray(middle+1, high))); }
相关文章推荐
- (转载)Android手机开发总结
- CSS布局(下)
- linq学习三个实例
- MySQL服务器的连接数
- 黑马程序员————————多线程(模拟动车售票案例)
- ORACLE 近期执行的sql
- Java编程思想之this关键字和static关键字
- PHP的模板Template模式
- Gentoo下打开windows txt文件中文乱码问题
- C#获取当前应用程序所在路径及环境变量
- 精通java之JVM详解(每日研读2次以上,对java理解必有提升)
- 微软Edge 内嵌的JavaScript 引擎即将开源
- CSS布局(上)
- 一个数学题目的小小算法
- 1:环境安装与介绍:canopy
- UTC标准时间
- 最全Pycharm教程(15)——Pycharm编辑器功能之自动生成格式
- Yii2.0 数据库操作
- ubuntu启动oracle服务步骤
- arcgis刚刚创建站点,就忘记用户名和密码了