题目:

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. Vue 需要使用jsonp解决跨域时,可以使用(vue-jsonp)

    1,执行命令 npm install vue-jsonp --save 2.src/main.js中添加: import VueJsonp from 'vue-jsonp' Vue.use(VueJs ...

  2. Yarn application has already exited with state FINISHED

    如果在运行spark-sql时遇到如下这样的错误,可能是因为yarn-site.xml中的配置项yarn.nodemanager.vmem-pmem-ratio值偏小,它的默认值为2.1,可以尝试改大 ...

  3. hdu 2149

    题目 巴什博奕(Bash Game) 巴什博奕(Bash Game):只有一堆n个物品,两个人轮流从这堆物品中取物,规 定每次至少取一个,最多取m个.最后取光者得胜. 显然,如果n=m+1,那么由于一 ...

  4. 安装及使用Eclipse Maven插件的经验

    Eclipse Maven插件的站点目前已经迁移到了Eclipse主站上:http://eclipse.org/m2e/ 其安装方法也非常简单,通过Eclipse访问下面的URL:http://dow ...

  5. [FRAMESET][PHP]Frameset下面使用php-header('location:...') redirect链接

    一般,我们的管理后台都是使用frameset来进行布局的,所以如果我们对后台的登录会话时间进行了设定,那么在超过该时间session失效之后,那么我们就必须要在php文件中进行判断处理. 判断会话失效 ...

  6. WPF 最简单的TextBox水印

    最简单的TextBox加水印的方法,但是不具有很强的通用性. 如果你只是使用一次,或者用的不多,偷偷懒可以使用. 因为此方法只需要修改TextBox的Template,而不用重写何任代码. 注意: 1 ...

  7. [ACM_动态规划] hdu1003 Max Sum [最大连续子串和]

    Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...

  8. 设计模式之工厂模式(Factory Pattern)

    一.什么是工厂模式? 1.“简单工厂模式”,Simple Factory Pattern 也就是常用的在Factory类中定义静态方法负责new对象的方式. 摘要中提到过“严格地说,这种被称为“简单工 ...

  9. C# 实现图片压缩

    代码: private static ImageCodecInfo GetImageCodecInfo(ImageFormat imageFormat) { ImageCodecInfo[] imag ...

  10. 673. Number of Longest Increasing Subsequence

    Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...