leetcode || 53、Maximum Subarray
problem:
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.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
thinking:
(1)这道题解法特别多:
方法1:将每个数和后一个数字相加,得到一个正负分布的序列,正数项对最大和子序列实用,作比較就可以
方法2:暴力匹配,两层循环,调用max()函数。时间复杂度O(n*n),也能够算出结果,可是提交超时
方法3:採用DP。时间复杂度O(n)
方法4:分治法。时间复杂度为nlog(n)
(2)本人实现了方法3 和方法4
code:
DP 法:
class Solution {
public:
int maxSubArray(int A[], int n) {
int sum=A[0];
int maxsum=A[0];
for(int i=1;i<n;i++)
{
if(sum<0) //DP核心
sum=0;
sum+=A[i];
maxsum=max(sum,maxsum);
}
return maxsum;
}
};
分治法:
class Solution {
public:
int maxSubArray(int A[], int n) {
int ret=maxsub(A,0,n-1);
return ret;
}
protected:
int maxsub(int A[], int start, int end)
{
int max_left=INT_MIN,max_mid=INT_MIN,max_right=INT_MIN;
if(start==end)
return A[start];
if(start+1==end)
{
int a=max(A[start],A[end]);
return a>(A[start]+A[end])?a:(A[start]+A[end]);
}
int mid=(start+end)/2;
int tmp_sum=A[mid];
max_mid=tmp_sum;
int i=mid-1;
int j=mid+1;
while(i>=start) //难点在于当连续最大和子序列分布在mid的一側或两側时,怎么处理
{
tmp_sum+=A[i];
i--;
max_mid=max(max_mid,tmp_sum);
}
if(max_mid>A[mid]) //推断是处于两側,还是处于一側
tmp_sum=max_mid;
else
tmp_sum=A[mid];
while(j<=end)
{
tmp_sum+=A[j];
j++;
max_mid=max(max_mid,tmp_sum);
}
max_left=max(max_left,maxsub(A,start,mid-1));//二分轮廓
max_right=max(max_right,maxsub(A,mid+1,end));
int tmp_max = max(max_left,max_right);
return max_mid>tmp_max?max_mid:tmp_max;
}
};
leetcode || 53、Maximum Subarray的更多相关文章
- (LeetCode 53)Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【算法】LeetCode算法题-Maximum Subarray
这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- 基于ASP.NET MVC和Bootstrap搭建响应式个人博客站(一)
1.0 为什么要做这个博客站? www.zynblog.com 在工作学习中,经常要搜索查找各种各样的资料,每次找到相关资料后都会顺手添加到浏览器书签中,时间一长,书签也就满了.而且下次再点击这个 ...
- HDU 5122 K.Bro Sorting
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Tot ...
- 类装载器ClassLoader
类装载器工作机制 类装载器就是寻找类的节码文件并构造出类在JVM内部表示对象的组件.在Java中,类装载器把一个类装入JVM中,要经过以下步骤: [1.]装载:查找和导入Class文件: [2.]链接 ...
- Long Dominoes(ZOJ 2563状压dp)
题意:n*m方格用1*3的方格填充(不能重叠)求有多少种填充方法 分析:先想状态,但想来想去就是觉得不能覆盖所有情况,隔了一天,看看题解,原来要用三进制 0 表示横着放或竖放的最后一行,1表示竖放的中 ...
- codeforces 687D Dividing Kingdom II 带权并查集(dsu)
题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...
- [Irving]字符串相似度-字符编辑距离算法(c#实现)
编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字 ...
- [原创]个人工具 - YE快速复制助手(YeFastcopyHelper)
版本:v1.3.216 更新时间:2014/02/16 * 代码完善 + 右键关于显示当前版本号,点击并链接到软件帮助页 Technorati 标签: NET,.NET 3.5,asion C#,Ch ...
- 一道JAVA经典面试题目的两种解法
题目要求:String s="-1 2 5 78 129 -65 -23";将字符串进行升序排序后输出. 方法一:使用数组进行排序 思路: 1.获取字符串中的数值: 2.将数组 ...
- select的option异常的总结
来源:http://www.ido321.com/1189.html 昨天,在项目中碰到了option显示异常的原因,截图如下: Firefox中用css控制之后效果 chrome和IE中css不奏效 ...
- 使用DBCC SHOW_STATISTICS展示索引的统计信息
在开始之前搭建演示环境: USE master GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassA', N'U') IS NOT NULL DROP TAB ...