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: ...
随机推荐
- C# 好用的三层架构,项目直接上手用
一.项目结构 注意:1.Common类中的引用添加:右键--添加引用--.NET--选择对应的引用 2.各层之间引用互相添加(这个就不必多说了,三层最基础部分) 3.在添加 Oracle 引用时候 ...
- android 官网处理图片 代码
/** * 获取压缩后的图片 (官网大图片加载对应代码) * * @param res * @param resId * @param reqWidth * 所需图片压缩尺寸最小宽度 * @param ...
- HDU5794 A Simple Chess 容斥+lucas
分析:转自http://blog.csdn.net/mengzhengnan/article/details/47031777 一点感想:其实这个题应该是可以想到的,但是赛场上并不会 dp[i]的定义 ...
- Linux时间与Windows差8个时区的问题解决方法
我的Debian7.1的时间与windows上的时间不一致,正好差8个时区,原因是Debian将机器的物理时间理解为UTC时间了.去网上找了好多文章,基本上都是说要改/etc/default/rcS, ...
- 用Python操作Mysql
平时的主要编程语言是Java,开发时也主要用Mysql,经常为了测试,调试的目的需要操作数据库,比如备份,插入测试数据,修改测试数据,有些时候不能简单的用SQL就能完成任务,或都很好的完成任务,用Ja ...
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- mysql 查看锁表解锁
-- 查看那些表锁到了show OPEN TABLES where In_use > 0;-- 查看进程号show processlist;--删除进程 kill 42236:
- uva202:循环小数(循环节+抽屉原理)
题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r ...
- SqlServer修改数据库文件及日志文件存放位置
--查看当前的存放位置 select database_id,name,physical_name AS CurrentLocation,state_desc,size from sys.master ...
- find in linux
find命令的作用是在目录中根据文件名搜索文件find 列出当前目录及其子目录的所有文件和文件夹的完整路径.find -name Help.java 在当前目录及其子目录中搜索文件名为Help.jav ...