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.

题意:给定由正负整数组成的数组,问和最大的子数组,返回和。
思路:这题和jump gamejump game ii 类似。定义两个变量,一个维护目前的最大值maxValue,一个是之前元素值的最大值sum,当sum小于0时说明,若算上之前的数会是整体的最大值减小,所以舍弃。时间复杂度为O(n)。代码如下:
 class Solution {
public:
int maxSubArray(int A[], int n)
{
if(n==) return ;
int sum=,maxValue=A[];
for(int i=;i<n;++i)
{
sum+=A[i];
maxValue=max(maxValue,sum);
if(sum<)
sum=;
}
return maxValue;
}
};

方法二:分治。参考Grandyang的博客。分治的思想类似二分搜索,首先将数组一分为二,分别找出左边和右边的最大子数组之和,然后从中间开始向左右分别扫描,求出的最大值分别和左右两边得到的最大值相比较,取最大值。

代码如下:

 class Solution {
public:
int maxSubArray(int A[], int n)
{
if(n==) return ;
return helper(A,,n-);
} int helper(int A[],int left,int right)
{
if(left>=right) return A[left];
int mid=(left+right)>>;
int lmx=helper(A,left,mid-);
int rmx=helper(A,mid+,right); int mMax=A[mid],temp=mMax; //遍历左半端,
for(int i=mid-;i>=left;--i)
{
temp+=A[i];
mMax=max(mMax,temp);
}
temp=mMax; //向右
for(int i=mid+;i<=right;++i)
{
temp+=A[i];
mMax=max(mMax,temp);
} return max(mMax,max(lmx,rmx));
}
};

[Leetcode] maximun subarray 最大子数组的更多相关文章

  1. [LeetCode] Maximum Subarray 最大子数组

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

  2. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  3. [leetcode]53. Maximum Subarray最大子数组和

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  4. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  5. 【LeetCode每天一题】Maximum Subarray(最大子数组)

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

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

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

  7. Maximum Subarray(最大子数组)

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

  8. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  9. [LeetCode] Maximum Product Subarray 求最大子数组乘积

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

随机推荐

  1. Chromium添加一段新字符串

    参考:https://groups.google.com/a/chromium.org/forum/#!searchin/chromium-dev/tclib%7Csort:relevance/chr ...

  2. python爬取视频网站m3u8视频,下载.ts后缀文件,合并成整视频

    最近发现一些网站,可以解析各大视频网站的vip.仔细想了想,这也算是爬虫呀,爬的是视频数据. 首先选取一个视频网站,我选的是 影视大全 ,然后选择上映不久的电影 “一出好戏” . 分析页面 我用的是c ...

  3. 180606-Linux下jdk中文乱码问题解决

    文章链接:https://liuyueyi.github.io/hexblog/2018/06/06/180606-Linux下jdk中文乱码问题解决/ linux下jdk中文乱码问题解决 之前遇到过 ...

  4. PAT - L2-001. 紧急救援( Dijstra )

    - PAT - L2-001. 紧急救援 题目链接 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两 ...

  5. (Python爬虫04)了解通用爬虫和聚焦爬虫,还是理论知识.快速入门可以略过的

    如果现在的你返回N年前去重新学习一门技能,你会咋做? 我会这么干: ...哦,原来这个本事学完可以成为恋爱大神啊, 我要掌握精髓需要这么几个要点一二三四..... 具体的学习步骤是这样的一二三.... ...

  6. django 增删改查操作 数据库Mysql

    下面介绍一下django增删改查操作: 1.view.py # -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom dja ...

  7. tomcat 运行机制

    先不去关技术细节,对一个servlet容器,我觉得它首先要做以下事情:1:实现Servlet api规范.这是最基础的一个实现,servlet api大部分都是接口规范.如request.respon ...

  8. [ML] the notes

    "Machine Learning is not who has the best algorithm that wins. It is who has the most data.&quo ...

  9. nodejs笔记--基础篇(一)

    Sublime Node.js开发环境配置 下载并安装Node.js安装包后再开始配置 1.先安装好Sublime Text 2 2.运行Sublime,菜单上找到Tools ---> Buil ...

  10. Alpha阶段中间产物——Thunder团队

    Part One 版本控制 git地址:https://git.coding.net/lick468/iReader.git Part Two 软件功能说明书 相关链接:http://www.cnbl ...