(LeetCode 53)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.
题目:
最大子数组和
思路:
1、暴力枚举
起点:i=0,...,n-1;终点:j=i,....,n-1;依次求[i,j]区间的和,时间复杂度O(n^3)
2、优化枚举
起点:i=0,...,n-1;终点:j=i,....,n-1;累计求[i,j]区间的和,时间复杂度O(n^2)
3、分治算法
分:两个等长的子数组,分别求解,复杂度O(nlogn)
合:求包含中间点的最大子数组之和,复杂度O(n)
时间复杂度:O(nlogn)
4、动态规划
假设dp[i]表示以a[i]结尾的最大子数组和,那么
状态转移方程:
dp[i]=max(dp[i-1]+a[i],a[i])
- 包含a[0,i-1]:dp[i-1]+a[i]
- 不包含a[0,i-1]:a[i]
初始值:
dp[0]=a[0]
复杂度:
时间复杂度:O(n),空间复杂度:O(n)
空间优化:
dp[i]只与dp[i-1]有关,因此状态转移方程优化为:
best=max(best+a[i],a[i])
其实这里的动态规划实现的是一种简单的逻辑,即前面的数组和大于0,则加上,小于或等于0,则放弃。
if(cur>0)
cur+=A[i];
else
cur=A[i];
5、前缀数组和
定义:sum[i]=a[0]+a[1]+...+a[i]
sum(A[i....j])=sum[j]-sum[i-1]
对于数组A,以A[i]结尾的最大子数组和为sum[i]-min(sum(k)),k=0...i-1,因此需保存每一步计算中的最小sum值。
依次计算以A[i]结尾的最大子数组和,然后保留其最大值即可,详见代码。
代码:
只实现分治、动态规划以及前缀和三种思路
1、分治
class Solution {
public:
int maxSubArray(int A[], int n) {
if(n==1)
return A[0];
int mid=n/2;
int left=maxSubArray(A,mid);
int right=maxSubArray(A+mid,n-mid);
int ans=max(left,right);
int cur=A[mid-1];
int tmp=cur;
for(int i=mid-2;i>=0;i--){
cur+=A[i];
if(cur>tmp)
tmp=cur;
}
cur=tmp;
for(int i=mid;i<n;i++){
cur+=A[i];
if(cur>tmp)
tmp=cur;
}
return max(ans,tmp);
}
};
2、动态规划
class Solution {
public:
int maxSubArray(int A[], int n) {
int cur=A[0];
int max=A[0];
for(int i=1;i<n;i++){
if(cur>0)
cur+=A[i];
else
cur=A[i];
if(cur>max)
max=cur;
}
return max;
}
};
class Solution {
public:
int maxSubArray(int A[], int n) {
int endhere=A[0];
int ans=A[0];
for(int i=1;i<n;i++){
endhere=max(endhere+A[i],A[i]);
ans=max(ans,endhere);
}
return ans;
}
};
3、前缀数组和
class Solution {
public:
int maxSubArray(int A[], int n) {
int sum=A[0];
int minSum=min(0,sum);
int ans=A[0];
for(int i=1;i<n;i++){
sum+=A[i];
ans=max(ans,sum-minSum);
minSum=min(minSum,sum);
}
return ans;
}
};
(LeetCode 53)Maximum Subarray的更多相关文章
- leetcode || 53、Maximum Subarray
problem: Find the contiguous subarray within an array (containing at least one number) which has the ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- 【算法】LeetCode算法题-Maximum Subarray
这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...
- (LeetCode 78)SubSets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- (LeetCode 72)Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 《从零开始学Swift》学习笔记(Day 53)——do-try-catch错误处理模式
原创文章,欢迎转载.转载请注明:关东升的博客 Swift 1.x的错误处理模式存在很多弊端,例如:为了在编程时候省事,给error参数传递一个nil,或者方法调用完成后不去判断error是否为nil, ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- cloudstack ssvm 管理地址不够造成无法启动修复过程
cloudstack日志记录: 上面已经提示了,管理ip没有了,造成这个原因很多,遇到过ssvm非正常关闭就有可能不释放IP慢慢把IP消耗掉.总之这肯定是BUG.按照上面的提示找到对应pod 和dc ...
- 【BZOJ 2007】 2007: [Noi2010]海拔 (平面图转对偶图+spfa)
2007: [Noi2010]海拔 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2504 Solved: 1195 Description YT市 ...
- 51nod 1962区间计数(单调栈加二分)
题目要求是求出两个序列中处于相同位置区间并且最大值相同的区间个数,我们最直观的感受就是求出每个区间的最大值,这个可以O(N)的求,利用单调栈求出每个数作为最大值能够覆盖的区间. 然后我们可以在进行单调 ...
- Redis 锁的实现方案
开发中不可避免的是碰到并发请求,在数据严谨性的要求不高时,我们也不需要做什么处理,但如果碰到数据严谨性非常高的时候(例如:用户金额,秒杀产品的库存...),我们就需要慎重处理了. 解决方案多种多样,下 ...
- [转]应用服务器ApacheSocketUnixthread
HttpClient容易忽视的细节——连接关闭 博客分类: java HttpClient client = new HttpClient(); HttpMethod method = new G ...
- [转]怎么把一个textview的背景图片设置成圆角的?
在drawable文件夹下新建一个文件设置背景样式代码:在drawable文件夹下面新建text_view_border.xml<?xml version="1.0" ...
- AMScrollingNavbar框架(自动隐藏导航栏)使用简介
AMScrollingNavbar框架是一个可以上拉隐藏导航栏和下拉显示导航栏的框架,这个开源框架的调用也很简单,本章节就给大家介绍一下这个框架的用法. 一.下载及导入框架 AMScrollingNa ...
- 函数中的 arguments 对象
JavaScript函数具有像数组一样的对象,这些对象称为arguments,与传递给函数的参数相对应.传递给JavaScript函数的所有参数都可以使用arguments对象来引用. 现在我们开始学 ...
- win8升级8.1提示卸载sentinel runtime drivers
Win8升级8.1时提示需卸载sentinel runtime drivers的解决方法 第一步:打开sentinelcustomer.safenet-inc.com/sentineldownload ...
- win7 系统装SQLServer2000 成功
昨天在win7上装SQLServer数据库,写一下体会.首先,如果以前安装的话,要删除干净.我也找了半天的网络资料.1.把原来SQLServer的安装目录 C:\Program Files\Micro ...