【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 ...
随机推荐
- 大学站防SQL注入代码(ASP版)
方法1: Replace过滤字符 解决方法:查找login.asp下的<from找到下边的类似username=request.Form(”name”) pass=request.Form(”p ...
- Working with Transactions (EF6 Onwards)
Data Developer Center > Learn > Entity Framework > Get Started > Working with Transactio ...
- SpringMVC应用
SpringMVC的核心是DispatcherServlet,这个Servlet充当SpringMVC的前端控制器. SpringMVC是多线程的吗??? 控制器是一个用于处理请求的Spring组件. ...
- PHP如何实现页面静态化
1.file_put_contents()函数 2.fwrite()函数 3.使用PHP内置缓存机制实现页面静态化-output_buffering
- 用jinja做了个E-Letter小项目
做了一个html E-Letter项目. 邮件模板采用jinja2, html 邮件内容生成简直太爽了. 整个项目开发只用了2个小时, 调试却花了大半天时间, 生成的邮件总是发不出去. 于是, 打开 ...
- hadoop安装实战(mac实操)
集群环境配置参考(http://blog.csdn.net/zcf1002797280/article/details/49500027) 参考:http://www.cnblogs.com/liul ...
- ZOJ 3711 Give Me Your Hand
Give Me Your Hand Time Limit: 2 Seconds Memory Limit: 131072 KB Special Judge BellyWhite a ...
- JSON 问题
{"statusCode":"300","message":"栏目插入出现故障==bannerInfoService.add 栏目 ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Swift3.0P1 语法指南——基础
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...