Description:

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.

Thoughts:

this problem was discussed by Jon Bentley (Sep. 1984 Vol. 27 No. 9 Communications of the ACM P885)

the paragraph below was copied from his paper (with a little modifications)

algorithm that operates on arrays: it starts at the left end (element A[1]) and scans through to the right end (element A[n]), keeping track of the maximum sum subvector seen so far. The maximum is initially A[0]. 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.

there is my java code:

package easy.array;

public class MaxSubArray {
public int maxSubArray(int[] nums){
int maxsofar = nums[0];
int maxtotal = nums[0];
for(int i = 1; i< nums.length;i++){
maxsofar = Math.max(maxsofar+nums[i], nums[i]);
maxtotal = Math.max(maxtotal, maxsofar);
}
return maxtotal;
} public static void main(String[] args){
int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
MaxSubArray max = new MaxSubArray();
int num = max.maxSubArray(nums);
System.out.println(num);
}
}

maxSubArray的更多相关文章

  1. MaxSubArray 最大子数列和

    public int maxSubArray(int[] A) { int newsum=A[0]; int max=A[0]; for(int i=1;i<A.length;i++){ new ...

  2. Algo: maxSubArray vs. maxProduct

    这两个问题类似,都可利用动态规划思想求解. 一.最大连续子序列和 https://leetcode.com/problems/maximum-subarray/description/ https:/ ...

  3. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. leetcode--Maximum Subarray

    题目链接:https://leetcode.com/problems/maximum-subarray/ 算法类型:动态规划 题目分析:最大序列和 代码实现: class Solution(objec ...

  5. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  6. 7九章算法强化班全解--------Hadoop跃爷Spark

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

  7. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  8. 全部leetcode题目解答(不含带锁)

    (记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.)   88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...

  9. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

随机推荐

  1. Ubuntu 16.04 LTS今日发布

    Ubuntu 16.04 LTS今日发布 Ubuntu16.04 LTS 发布日期已正式确定为 2016 年 4 月 21 日,代号为 Xenial Xerus.Ubuntu16.04 将是非常受欢迎 ...

  2. 2015年CSDN博客排名第一名,何方神圣?

    2015年CSDN博客排名第一名,何方神圣? 一.引子: 话说博主phphot,雄霸天下好多年. 俱往矣, 落花流水春去也. 斗转星移,江山易主. 详细可以参见下文: CSDN博客排名第一名,何许人也 ...

  3. 安装mysql到服务器的linux环境下

    1·安装mysql 命令:yum -y install httpd php mysql mysql-server 2·配置mysql 配置开机启动服务 /sbin/chkconfig --add my ...

  4. naoting

    生活就像一锅菠菜汤 20160714 夜

  5. ISLR系列:(1)线性回归 Linear Regression

       Linear Regression 此博文是 An Introduction to Statistical Learning with Applications in R 的系列读书笔记,作为本 ...

  6. 09_EGIT插件的安装,Eclipse中克隆(clone),commit,push,pull操作演示

     1 下载EGIT,下载地址:http://www.eclipse.org/egit/download/ 最终的下载地址: http://www.eclipse.org/downloads/dow ...

  7. linux下播放组播流出现setsockopt:No such device错误

    在linux下播放组播流出现setsockopt:No such device错误是因为多播IP没有add路由表里面 可以采用如下命令完成: root@android:/ # busybox rout ...

  8. BezierDemo开源项目的学习

    多看多学涨姿势,no zuo nuo die做暖男 1.概述 国际惯例,首先感谢一下开源作者. 这个项目主要是实现实现qq红点拖拽的效果 地址在https://github.com/chenupt/B ...

  9. ITU-T E.800 有关服务质量(QoS)的术语定义

    摘要 ITU-T E.800建议书为服务质量(QoS)的研究和管理提供了一套通用术语.本建议书列出的与QoS相关的技术和非技术术语旨在代表电信市场所有各方(即用户.服务提供商.制造商和监管机构)的利益 ...

  10. Android开发常用网站汇总

    1.eoe Android开发者论坛 目前国内最早的Android开发者社区,人气非常旺聚集了不少Android开发方面的高手,开发中遇到的问题大都能在这里获得解决,网站最大的特色是定期发布<e ...