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的情况下 ...
随机推荐
- Js获取fileupload的绝对路径时总是的到C:\fakepath\+文件名称的 解决方案
解决方法: Internet选项->安全->自定义级别->将文件下载到服务器时包含本地目录路径 启用就可以了.
- ubuntu 安装 桌面 awesome
受了ubuntu 12.04自带的桌面,运行太卡了 http://www.linuxzen.com/awesometmuxgnomedoda-zao-gao-xiao-linuxzhuo-mian-h ...
- sass 入门教程
1.引言 众所周知css并不能算是一们真正意义上的“编程”语言,它本身无法未完成像其它编程语言一样的嵌套.继承.设置变量等工作.为了解决css的不足,开发者们想到了编写一种对css进行预处理的“中间语 ...
- sqlserver字符串转日期
declare @str varchar(15) declare @dt datetime select @str='2005-8-26' set @d ...
- JavaScript—W3school
一.JavaScript基础 1.写入HTML输出 2.对事件作出反应 3.改变HTML内容 4.改变HTML图像 5.改变HTML样式 6.验证输入 <script> Function ...
- PHP生成制作验证码
看完就会,不会你打我,话不多说.开搞(人狠话不多) 1.0 首先先看代码 <?php header("Content-Type:text/html;Charset=UTF-8" ...
- php内存管理
1.为什么需要内存管理 由于计算机的内存由操作系统进行管理,所以普通应用程序是无法直接对内存进行访问的, 应用程序只能向操作系统申请内存,通常的应用也是这么做的,在需要的时候通过类似malloc之类的 ...
- 【java版坦克大战---准备篇】 java 绘图
要写坦克大战当然要先画出坦克.java画图是基础. package com.game; import java.awt.*; import javax.swing.*; public class Pr ...
- Linux使用问答
1.ubuntu 查看安装的软件包? 在终端输入 sudo dpkg -l http://vardesa.blog.hexun.com/58593247_d.html 其他:http://qiuye. ...
- Tekla Structures 使用类库概览
Tekla Structures 2016 已经发布了,使用了 Ribbon 的全新 UI 风格,比以前要漂亮许多. 不过功能方面貌似没啥大的改进,感觉天宝的主要精力都投入到了混凝土模块上,忙着和别人 ...