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: ...
随机推荐
- MOSS 2010:Visual Studio 2010开发体验(14)——列表开发之事件接收器
转:http://boke.25k5.com/kan141919.html 通过前面几篇,我们已经完成了内容类型,列表定义,列表实例g 8h"@的开发.本篇继续讲解列表中的一个重要环节- ...
- LoadRunner检查点实战
码农博客 即将到期,现将博客中部分文章转载到博客园.转载时略有删减. 一.为什么要使用检查点 为什么要使用检查点,那就要说明一下LR如何判断脚本是否执行成功. LR判断脚本是否执行成功是根据服务器返回 ...
- CSS、CSS2和CSS3选择器总结(全部选择器种类及其优先级)
选择器种类罗列: 1.基础的选择器 选择器 含义 示例 * 通用元素选择器,匹配任何元素 * { margin:0; padding:0; } E 标签选择器,匹配所有使用E标签的元素 p { fon ...
- ASP.NET 中JSON 的序列化和反序列化
JSON是专门为浏览器中的网页上运行的JavaScript代码而设计的一种数据格式.在网站应用中使用JSON的场景越来越多,本文介绍ASP.NET中JSON的序列化和反序列化,主要对JSON的简单介绍 ...
- ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决
一般情况在使用ASP.NET开发使用JavaScript有中文汉字时不会出现乱码情况,比如:alert('您看到我了吗?');这样直接输入中文汉字的代码中是不会出现乱码的,如果出现了,一是检查Web. ...
- bzoj 3997 [TJOI2015]组合数学(DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...
- Python的列表排序
Python的列表排序 本文为转载,源地址为:http://blog.csdn.net/horin153/article/details/7076321 在 Python 中, 当需要对一个 list ...
- Unity3d自定义脚本模板
这是一个小技巧,打开Unity安装目录,如: C:\Program Files (x86)\Unity\Editor\Data\Resources\ScriptTemplates /* * * Tit ...
- Spark系列(六)Master注册机制和状态改变机制
各组件的注册流程如下图: 注册机制源码说明: 入口:org.apache.spark.deploy.master文件下的receiveWithLogging方法中的case RegisterAppli ...
- ffmpeg内置aac编码器正式发布
https://www.ffmpeg.org/#aac_encoder_stable February 15th, 2016, FFmpeg 3.0 "Einstein" FFmp ...