【leetcode】Maximum Subarray
Maximum Subarray
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.
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.
class Solution {
public:
int maxSubArray(int A[], int n) {
int sum,maxSum;
sum=maxSum=A[];
for(int i=;i<n;i++)
{
if(sum<) sum=;
sum+=A[i];
if(maxSum<sum) maxSum=sum;
}
return maxSum;
}
};
class Solution {
public:
int maxSubArray(int A[], int n) {
divideAndConquer(A,,n-);
}
int divideAndConquer(int A[],int left,int right)
{
if(left==right) return A[left];
int mid=(left+right)/;
int leftMax=divideAndConquer(A,left,mid);
int rightMax=divideAndConquer(A,mid+,right);
int midSum1=;
int midMax1=A[mid];
for(int i=mid;i>=left;i--)
{
midSum1+=A[i];
if(midMax1<midSum1) midMax1=midSum1;
}
int midSum2=;
int midMax2=A[mid+];
for(int i=mid+;i<=right;i++)
{
midSum2+=A[i];
if(midMax2<midSum2) midMax2=midSum2;
}
int midMax=midMax1+midMax2;
return max(max(leftMax,rightMax),midMax);
}
};
【leetcode】Maximum Subarray的更多相关文章
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- 【转】SPDY协议
SPDY协议 - v3 原文:SPDY Protocol - Draft 3 翻译:邱鹏滔(QQ: 95350530,主页:www.fireflysource.com) 1 概述 HTTP协议的瓶颈在 ...
- realloc,malloc,calloc函数的区别
from:http://www.cnblogs.com/BlueTzar/articles/1136549.html realloc,malloc,calloc的区别 三个函数的申明分别是: void ...
- 如何理解clear的css属性?
参考文章: http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html clear: 只影响使用 clear样式属性的 元素本身, ...
- [译]A Beginner’s Guide to npm — the Node Package Manager
原文: http://www.sitepoint.com/beginners-guide-node-package-manager/ Installing Node.js 验证你的安装是否成功. $ ...
- 基础知识系列☞C#中数组Array、ArrayList和List三者的区别
数组() #region 数组 //初始化方式_0:先声明再赋值 ]; weekDays_0[] = "Sun"; weekDays_0[] = "Mon"; ...
- [转载]onkeydown、onkeypress、onkeyup、onblur、o
转载链接: http://blog.sina.com.cn/s/blog_697b2dc101014ktb.html onkeydown:按下任何键(字母.数字.系统.tab等)都能触发,且对于字母不 ...
- 利用百度语音API进行语音识别。
由于项目需要,这几天都在试图利用百度语音API进行语音识别.但是识别到的都是“啊,哦”什么的,我就哭了. 这里我只是分享一下这个过程,错误感觉出现在Post语音数据那一块,可能是转换问题吧. API请 ...
- VBA 表格操作2 表格复制
office excel文件有工作簿,里面存放一张张表,表的名字叫做标签名,簿名就是我们常见的的文件名,簿的类型有"一簿一表"与"一簿多表". 完成将多张表合并 ...
- 大数据之nutch
一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...
- CentOS编译安装Apache 2.4.x时报错:configure: error: Bundled APR requested but not found at ./srclib/. Download and unpack the corresponding apr and apr-util packages to ./srclib/.
先前按照这篇文章“CentOS6.x编译安装LAMP(2):编译安装 Apache2.2.22”去编译安装Apache2.2.x版本时,安装得挺顺利,今天换成Apache2.4.x版本,安装方法一样, ...