Leetcode52.Maximum Subarray(贪心与分治)
题目描述:
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6
题目分析:
该题属于Array类别,通过贪心可以实现O(n)级别的解决思路,通过分治法可以实现O(nlogn)的解决思路。
贪心法O(n)解决思路:
对于序列[A1,A2,...,AN]的遍历中,我们要求序列AI+...+AJ的和最大。
贪心算法在该问题的应用体现在我们在对序列遍历(i)求和的过程中,同时比较序列A0,...Ai的和与Ai的大小关系,两者的max值作为遍历至i时刻的当前序列最大sum.
并将当前序列最大sum与实际最大值比较,当序列遍历完毕,我们则求得整个序列的最大子集和。
Runtime: 72 ms, faster than 91.17% of Python3 online submissions for Maximum Subarray.
Memory Usage: 14.6 MB, less than 5.02% of Python3 online submissions for Maximum Subarray.
length = len(nums)summax = nums[0]cummax = nums[0]for num in nums[1:]:cummax = cummax+numif cummax <num:cummax = numif cummax >summax:summax = cummaxreturn summax
分治法O(nlogn)解决思路:
分治算法一般实现分为三个步骤:
(1)划分问题:将问题的实例划分为子问题,在本题中,就是将长度为l的序列不断划分为l/2的序列问题;
如划分中点m = l +int((r-l)/2)
在这里,需要注意区间的划分问题,在本题中,我采用较常用的左闭右开的区间划分方式,这样子在处理数组分割的问题比较自然。
对于一个长度为L的数组问题,我们视为区间[0,L),在第一次递归分治的划分过程中,分成[0,m) [m,L)
区间划分问题决定了我们如何确定递归终止条件
如划分为左闭右开:
对于任何长度大于等于2的数组,我们的递归终止条件为left-right==1
对于长度为1的数组,我们直接设定递归终止条件为left==right
(2)递归求解:递归解决子问题,在本题中,通过递归调用maxsub(left,m) maxsub(right,m),求得左右各子区间的最大序列sum值。递归终止条件在(1)中已提;
(3)合并问题:在(2)中我们已求得左右各子区间的最大序列sum值,接下来我们要考虑:左右子区间之间是否存在更大的序列sum值。
则我们需要从当前中点m为起点,向左遍历至left,求得最大的leftsummax
向右遍历至right-1,求得最大的rightsummax,
在将leftsummax+rightsummax与(2)中的最大值比较,作为当前递归的最后结果。
如果leftsummax+rightsummax大于(2)中的最大值,则说明左右子区间,包含中间m点在内的子序列存在更大的序列sum值
Runtime: 164 ms, faster than 5.83% of Python3 online submissions for Maximum Subarray.
Memory Usage: 14.8 MB, less than 5.02% of Python3 online submissions for Maximum Subarray.
class Solution:def maxsubhelper(self,nums,l,r):if r-l==1:return nums[l]if r==l:return nums[l]m = l + int((r-l)/2)maxtmp = max(self.maxsubhelper(nums,l,m),self.maxsubhelper(nums,m,r))leftmax=0leftsummax = nums[m-1]for i in range(m-1,l-1,-1):leftmax += nums[i]leftsummax = max(leftsummax,leftmax)rightmax = 0rightsummax = nums[m]for i in range(m,r):rightmax += nums[i]rightsummax = max (rightsummax,rightmax)return max(maxtmp,leftsummax+rightsummax)def maxSubArray(self, nums: List[int]) -> int:return self.maxsubhelper(nums,0,len(nums))
Leetcode52.Maximum Subarray(贪心与分治)相关推荐
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- Lintcode42 Maximum Subarray II solution 题解
[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray
41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...
- Leetcode-53 Maximum Subarray
#53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number ...
- 53.Maximum Subarray
/** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...
- 【数据结构与算法】【算法思想】【联系与区别】回溯 贪心 动态规划 分治
4种算法思想比较与联系 如果将贪心,分治,回溯和动态规划四种算法思想分类,那贪心,回溯,动态规划可归为一类,而分治单独可以作为一类,因为它跟其他是三个都不大一样. 因为前三个算法解决问题的模型,都可以 ...
- LeetCode(53):Maximum Subarray
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
最新文章
- 小型自动化运维--expect脚本之自动同步
- php zip.so 编译出错,php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解决方法...
- leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
- 天池读书会六月场来啦,零基础也能听的懂的编程分享!
- P6178-[模板]Matrix-Tree 定理
- Mybatis insert返回主键ID
- python下载教程-Python 如何入门?附Python教程下载
- [转]编程的首要原则(s)是什么?
- 深入理解函数中分配内存的问题
- Robotics 机器人运动学 DH参数建模
- 基于electron和ffmpeg下载rtmp直播流
- 10分钟免费邮箱:10minutemail
- BIOS 从FFFF0H处开始执行指令的理解
- U盘安装win8.1
- 【干货】新显卡太贵,便宜老卡怎么选?二手亮机卡过渡指南!
- 大一的第三次作业,/(ㄒoㄒ)/~~
- 一文弄懂JVM内存结构,垃圾回收器和垃圾回收算法
- 2021年登高架设考试题及登高架设考试内容
- 悼念 Benoit MandelBrot
- 牛客算法周周练14 BCircle D 绝地求生(gcd,思维) E