4

leetCode解题报告之O(n)线性时间求最大子序列和(Maximum Subarray)

 3 years ago
source link: https://blog.csdn.net/ljphhj/article/details/22045373
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

leetCode解题报告之O(n)线性时间求最大子序列和(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.

click to show more practice.

分析:

给定一整数序列A1, A2,... An (可能有负数),求A1~An的一个子序列Ai~Aj,使得Ai到Aj的和最大 
例如:整数序列[−2,1,−3,4,−1,2,1,−5,4]的最大子序列[4,−1,2,1] 的和为6,。

解题思路:

1.我们最容易想到的就是暴力法来求解了,我们先来看看两种穷举法的写法和时间复杂度(分别为O(n^3) 和 O(n^2))

2.我会使用分治法来完成这道题目,复杂度为O(nlogn)

3.我会使用DP的思想来做这道题目,复杂度为O(n)

一、穷举法(暴力)【O(n^3)】

二、穷举法的改进(暴力)【O(n^2)】

三、分治法(二分法)【O(nlogn)】(这里要怒喷一下,网上好多人都有一份分治法的代码,都是copy来copy去的,但是却不是正确的,这代码是AC过的才发上来的!)

思路:由于我们知道最大子序列可能存在于A数组的左边,右边,或者一点左边一点右边。

所以我们很容易可以联想到,居然这样我们可以把A数组划分成若干个小的子数组,对子数组求出左边的最大值,和右边的最大值,再求出从中间位置到左边的某个位置的最大值、从中间位置到右边的某个位置的最大值,得到了这四个值之后剩下的我们就可以通过比较得到这个子数组的最大值了。(递归的过程)

四、DP思想解题【O(n)】(这里要怒喷一下,网上好多人都有一份分治法的代码,都是copy来copy去的,但是却不是正确的,这代码是AC过的才发上来的!)

看到这里了,留个言呗O(∩_∩)O,欢迎提出不足的地方!!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK