Get the largest sum of contiguous subarray in an int array
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的更多相关文章
- [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 ...
- [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 ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 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 ...
- 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 ...
- [Algorithm] Maximum Contiguous Subarray algorithm implementation using TypeScript / JavaScript
Naive solution for this problem would be caluclate all the possible combinations: const numbers = [1 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
随机推荐
- wamp配置
# #localhost # <VirtualHost *:> DocumentRoot "D:/software/wamp/www" ServerName local ...
- HTML5学习笔记----html5与传统html区别
一. HTML5语法的改变 该知识点所说变化指的是基于HTML4基础上所定义的改变,主要有如下: HTML5的文件扩展符(.html或.htm)与内容类型(text/html)保持不变. HTML5中 ...
- windows server 2008 R2 忘记administrator密码
第一步: 插入安装光盘,重光驱启动系统,在选择“安装语言”的地方,按shift+F10 在弹出的CMD窗口中,输入以下地址: x:\>c: c:\>cd windows\system32 ...
- We7在政府门户中的应用
政府门户从传统的信息引导发展到现阶段的服务型门户,不论从角度转变上还是从平台选型上都跟以前有很大的不同,其更注重的是安全.扩展.易用和移动互联网几部分(当然这儿的注重是建立在已有政府门户电子政务三个板 ...
- App页面显示优化
在开发移动端APP页面时,对各操作系统各种型号的手机进行适配是必须的.然鹅,上周在开发完一个落地页后,被测试给打了回来,其中列出了一个在我看来很小的问题:单击进入页面的时候,页面还没加载完的时候字体显 ...
- iphone 与 ipad -- UIPopoverPresentationViewController
iOS8.0之后, 苹果推出了UIPopoverPresentationViewController, 在弹出控制器时, 统一采用 presentViewController, 但是要实现iPhone ...
- 用19种编程语言写Hello World
用19种编程语言写Hello World 转载自:http://www.admin10000.com/document/394.html Hello World 程序是每一种编程语言最基本的程序,通常 ...
- WordPress 使用 Pie-Register 添加前台注册、登录、找回密码和编辑个人资料功能
转自:http://www.wpdaxue.com/front-end-publishing.html Pie-Register 是一个功能比较完善的 WordPress 才能,使用它可以很方便添加和 ...
- SQLMAP系列教程
1.SQLMAP安装及access注入: http://www.stronkin.com/en/CompHonorBig.asp?id=7 2.Mysql数据库注入 http://www.slib ...
- ASP.NET Web API 2 入门(一)
前言 HTTP 不是只是为了服务的 web 页.这也是建设公开服务和数据的 Api 的强大平台.HTTP 是简单的. 灵活的和无处不在.你能想到的几乎任何平台有 HTTP 库,因此,HTTP 服务可以 ...