题目:

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. mysql图文安装教程(win7 32位 亲测)

    一.下载mysql:http://www.mysql.com/downloads/ 弹出: 你需要有一个 Oracle Web 帐户,没有的话,注册一个: 勾选许可: 输入搜索条件: 下载MySQL ...

  2. MemCahced 使用及常见问题说明

    前言 本文档是针对Memcached使用及常见问题的说明. 一.获取 1. MemCached 官网:http://www.memcached.org 下载:http://memcached.org/ ...

  3. java web开发过程中的“\”指的是什么,如何区分

  4. java使用properties文件

    使用对用的util包下的properties包就可以了,这样我们有配置的话,写到一个properties文件中更直观. 这里写一个比较丑的例子: package com.property; impor ...

  5. Android Studio 集成 TFS,实现安卓移动开发的持续集成和交付(DevOps)

    目录 1 集成TFS系统.... 1.1 概述.... 1.2 安装TFS插件.... 1.2.1 在线安装方式.... 1.2.2 离线安装方案.... 1.3 常见操作.... 1.3.1 新建G ...

  6. Spring Boot 2 实践记录之 Powermock 和 SpringBootTest

    由于要代码中使用了 Date 类生成实时时间,单元测试中需要 Mock Date 的构造方法,以预设其行为,这就要使用到 PowerMock 在 Spring Boot 的测试套件中,需要添加 @Ru ...

  7. BitAdminCore框架应用篇:(五)核心套件querySuite列的定义

    索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...

  8. C#跨线程操作UI

    WPF启动调度器 : Dispatcher.Invoke(new Action(() => { //你的代码 }));

  9. sqlServer数据库纵横表相互转化

    sqlServer  数据库纵横表相互转化 一.纵表转横表: 1.纵表: 2.横表: 3. 代码: select Name as '姓名', end) as '语文', end) as '数学', e ...

  10. Redis安装步骤 - linux系统下

    https://blog.csdn.net/lzj3462144/article/details/70973368 https://www.cnblogs.com/pyyu/p/9467279.htm ...