When I finished reading this problem,I thought I could solve it by scanning every single subarray in the array,and the time complexity is cubic.Every subarray could be the eventual one whose sum is the largest,so I did make a conclusion that the best time complexity is quadratic,and the only optimization is to reduce the times of addition,because I can keep track of the sum from array[i] to array[j] with a table(say table[arraylength][arraylength]).But even this DP-like skill can not reduce the time complexity to O(n).Then my friend told me there are room to optimize it,and the best time complexity is right O(n).

  My first reaction is HOW.After think it over,I found I just miss something tricky.In the process of iteration,we could hit a position whose subarray sum is negative(say i,then array[0]+...+array[i]<0),then we can and should throw this part away.It is because negative sum would contribute negative factor to the following sum we are to look for.With this cool pattern,the time complexity can be optimized to O(n).The below is my code,please let me know if there is anything wrong.Thanks.

 /*
author:zhouyou
date:2014/6/2
*/
#include <iostream>
#include <limits>
#include <algorithm> using namespace std; int GetMaxContiguousSubarraySum(int array[],const int arraylength)
{
if(!array || arraylength<=){
return numeric_limits<int>::min();//for error,return minimum of int
} int current_max = array[];
int Ret = array[];
for(int i=;i<arraylength;++i){
current_max = max(array[i],current_max+array[i]);
Ret = max(current_max,Ret);
}
return Ret;
} int main()
{
int array[] = {,,,-,,};
cout << GetMaxContiguousSubarraySum(array,) << endl;
return ;
}

Get the largest sum of contiguous subarray in an int array的更多相关文章

  1. [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大

    17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...

  2. [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

    18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...

  3. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  4. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  5. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  6. [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript

    Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...

  7. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

随机推荐

  1. 【实习记】2014-09-24万事达卡bin查询项目总结

            8月28号,接到这个问题:现有前缀查询速度较慢,改进此知值求区间问题. 一开始没想到用二分法,更没有想到这个项目用了一个月,这一个月里,我学习并使用了middle框架写出了server ...

  2. Ubuntu下gcc及g++环境配置

    直接在命令行中输入以下命令即可. sudo apt-get install build-essential 安装完成后输入 gcc 和 g++ 进行确认.

  3. 【搭建开发环境】在 Windows XP 中参与开源项目,搭建 git 和 cygwin 开发环境

    引言 只有一台 Windows XP 家用机,却想在诸如 Git@OSC 之类的开源社区参与开发,本文提供一个入门级的开发环境搭建指引. 涉及工具:Eclipse,EGit,Cygwin. 欢迎来到 ...

  4. @font-face

     /**  * jQuery.hhNewSilder 滚动图片插件  * User: huanhuan  * QQ: 651471385  * Email: th.wanghuan@gmail.com ...

  5. linux 配置apache+subversion

    http://apr.apache.org/download.cgi http://subversion.tigris.org/servlets/ProjectDocumentList?folderI ...

  6. linux服务器监控流量sh脚本

    服务器可能经常遇到服务器出带宽跑满,不知如何查询被哪个进程占用的情况,有一款开源的英文软件iftop功能比较强大可以查询相关信息,可能刚接触linux系统的朋友不太会使用,在此写了一个功能比较简单无需 ...

  7. Entity Framework Code First 数据迁移

    需要在[工具 --> NuGet 程序包管理器 --> 程序包管理器控制台]中输入三个命令: Enable-Migrations (初次迁移时使用) Add-Migration [为本次迁 ...

  8. (转) 各种好用的插件 Xcode

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...

  9. 国内物联网平台初探(八):移动云OneNET

    国内物联网平台初探(八)——移动云OneNET 平台架构 数据模型 使用场景示意图 服务 IOT Paas 基础服务 为IoT开发者提供智能设备自助开发工具.后台技术支持服务,为您提供物联网专网.短彩 ...

  10. SecureCRT 绝佳配色方案, 保护你的眼睛

    http://blog.csdn.net/zklth/article/details/8937905   关键词:SecureCRT配色, SecureCRT设置颜色, Linux终端配色,Linux ...