题目:

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.

思路:

方法一:动态规划, 数组为vec[],设dp[i] 是以vec[i]结尾的子数组的最大和,对于元素vec[i+1], 它有两种选择:a、vec[i+1]接着前面的子数组构成最大和,b、vec[i+1]自己单独构成子数组。则dp[i+1] = max{dp[i]+vec[i+1],  vec[i+1]}

附加:记录左右节点位置

/**
* @param {number[]} nums
* @return {number}
*/
var maxSubArray = function(nums) {
var sum=0,maxsum=-2147483648,begin=0;
for(var i=0,len=nums.length;i<len;i++){
if(sum>=0){
sum=sum+nums[i];
}else{
sum=nums[i];
begin=i;
} if(maxsum<sum){
maxsum=sum;
left=begin;
right=i;
}
} return maxsum;
};

方法二

最简单的就是穷举所有的子数组,然后求和,复杂度是O(n^3)

int maxSum1(vector<int>&vec, int &left, int &right)
{
int maxsum = INT_MIN, sum = ;
for(int i = ; i < vec.size(); i++)
for(int k = i; k < vec.size(); k++)
{
sum = ;
for(int j = i; j <= k; j++)
sum += vec[j];
if(sum > maxsum)
{
maxsum = sum;
left = i;
right = k;
}
}
return maxsum;
}
 

算法三:

上面代码第三重循环做了很多的重复工作,稍稍改进如下,复杂度为O(n^2)

int maxSum2(vector<int>&vec, int &left, int &right)
{
int maxsum = INT_MIN, sum = ;
for(int i = ; i < vec.size(); i++)
{
sum = ;
for(int k = i; k < vec.size(); k++)
{
sum += vec[k];
if(sum > maxsum)
{
maxsum = sum;
left = i;
right = k;
}
}
}
return maxsum;
}
算法四:
分治法, 下面贴上编程之美的解释, 复杂度为O(nlogn)

//求数组vec【start,end】的最大子数组和,最大子数组边界为[left,right]
int maxSum3(vector<int>&vec, const int start, const int end, int &left, int &right)
{
if(start == end)
{
left = start;
right = left;
return vec[start];
}
int middle = start + ((end - start)>>);
int lleft, lright, rleft, rright;
int maxLeft = maxSum3(vec, start, middle, lleft, lright);//左半部分最大和
int maxRight = maxSum3(vec, middle+, end, rleft, rright);//右半部分最大和
int maxLeftBoeder = vec[middle], maxRightBorder = vec[middle+], mleft = middle, mright = middle+;
int tmp = vec[middle];
for(int i = middle-; i >= start; i--)
{
tmp += vec[i];
if(tmp > maxLeftBoeder)
{
maxLeftBoeder = tmp;
mleft = i;
}
}
tmp = vec[middle+];
for(int i = middle+; i <= end; i++)
{
tmp += vec[i];
if(tmp > maxRightBorder)
{
maxRightBorder = tmp;
mright = i;
}
}
int res = max(max(maxLeft, maxRight), maxLeftBoeder+maxRightBorder);
if(res == maxLeft)
{
left = lleft;
right = lright;
}
else if(res == maxLeftBoeder+maxRightBorder)
{
left = mleft;
right = mright;
}
else
{
left = rleft;
right = rright;
}
return res;
}

【数组】Maximum Subarray的更多相关文章

  1. [leetcode53]最长子数组 Maximum Subarray Kadane's算法

    [题目] Given an integer array nums, find the contiguous subarray (containing at least one number) whic ...

  2. LeetCode 53. Maximum Subarray(最大的子数组)

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

  3. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  4. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

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

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

  6. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  7. LeetCode: Maximum Product Subarray && Maximum Subarray &子序列相关

    Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least on ...

  8. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

    Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) whic ...

  9. Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum

    这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...

  10. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

随机推荐

  1. 学习前端的菜鸡对JS 的classList理解

    classList 在早期的时候要添加,删除类 需要用className去获取,然后通过正则表达式去判断这个类是否存在. 代码上去会有点麻烦,现在有了classList 就方便了很多. ——————— ...

  2. day04(权限修饰符,内部类,局部内部类,匿名内部类)

    权限修饰符, Public  >protected >default > private public 公共权限   随便都可以访问 protected  子类可以访问权限  (子类 ...

  3. hdu5883 The Best Path 2016-09-21 21:31 92人阅读 评论(0) 收藏

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  4. PAT甲 1032. Sharing (25) 2016-09-09 23:13 27人阅读 评论(0) 收藏

    1032. Sharing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To store Engl ...

  5. Firemonkey里触发home按键被按下的事件

    吾八哥我最近在使用Delphi里的Firemonkey平台写一个叫“由由密码管家”的APP工具,是跨多平台的,如ios/android/windows/macOs.由于是用于密码管理的,那么在手机里操 ...

  6. Delphi for iOS开发指南(4):在iOS应用程序中使用不同风格的Button组件

    http://blog.csdn.net/DelphiTeacher/article/details/8923481 在FireMonkey iOS应用程序中的按钮 FireMoneky定义了不同类型 ...

  7. [Openwrt 项目开发笔记]:DDNS设置(五)

    [Openwrt项目开发笔记]系列文章传送门:http://www.cnblogs.com/double-win/p/3888399.html 正文: 在上一节中,我主要讲述了如何在Openwrt上安 ...

  8. C# npoi 从excel导入datagridviews 批量联网核查

    DataSet ds = new DataSet(); OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Fil ...

  9. List泛型集合对象排序

    本文的重点主要是解决:List<T>对象集合的排序功能. 一.List<T>.Sort 方法 () MSDN对这个无参Sort()方法的介绍:使用默认比较器对整个List< ...

  10. svn 连接超时,连接失败解决办法

    1.确认服务是否开启 2.Windows防火墙是否开启,如开启则关闭防火墙 3.安全软件是否将3306与443端口关闭. 关闭后无法连接