输入一个长度为n的整型数组array,数组中的一个或连续多个整数组成一个子数组,子数组最小长度为1。求所有子数组的和的最大值。
数据范围:
要求:时间复杂度为
,空间复杂度为
进阶:时间复杂度为
,空间复杂度为 )
[1,-2,3,10,-4,7,2,-5]
18
经分析可知,输入数组的子数组[3,10,-4,7,2]可以求得最大和为18
[2]
2
[-10]
-10
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self,a): # write code here dp = [0]*len(a) dp[0]=a[0] for i in range(1,len(a)): if (dp[i-1]+a[i])<a[i]: dp[i]=a[i] else: dp[i]=dp[i-1]+a[i] return max(dp)
class Solution: def FindGreatestSumOfSubArray(self, array): # write code here if not array: return None n=len(array) res=[] for i in range(1,n+1): res.append(self.sumOfsubArray(array, i)) return max(res) def sumOfsubArray(self,array,k): if not array: return None n=len(array) res=[] for i in range(0,n-k+1): res.append(sum(array[i:i+k])) return max(res)
# -*- coding:utf-8 -*- class Solution: # 注意这里的首个数字不一定从0个开始 def FindGreatestSumOfSubArray(self, array): if len(array) < 1: return [] elif len(array) == 1: return array[0] else: ret = [] i = 0 n = len(array) while i < n: # i在增加,况且array每次弹出一个元素len(array)依旧是原始值,这里不可以递减 lst = [] # lst列表在每次开始循环的时候清空 for tmp in array: lst.append(tmp) ret.append(sum(lst)) # 要把所有可能的sum结果加入ret,ret要在循环之外 array.pop(0) # 不一定从首位开始,所以更换起始值 i += 1 return max(ret) # 要么i增加,array不变,要么array减少,len不变
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): # write code here res = max(array) tmp = 0 for i in array: tmp = max(i,tmp+i) res = max(tmp,res) return res注意:tmp = max(i,tmp+i)为什么不是max(tmp,tmp+i),是因为如果tmp在进入此次循环之前,已经和res比较过了,所以如果上一轮的tmp大,也会通过res保存下来,不用担心丢失的问题。
1.子问题拆分:表示第
个字符结尾的的最大子序和,状态
有三种情况:
个和加上
,
,
中的最大值,其值为
2.其实就是针对前个的最大和选择和不选择的问题,在位置
处三种选择,加上前
项的最大和,从
开始从新加,从
开始从新算
def maxSubArray(nums):
if not nums:
return 0
dp = [0]*len(nums)
dp[0] = nums[0]
for i in range(1,len(nums)):
tmp_sum = 0
tmp_sum = max(dp[i-1]+nums[i],nums[i]+nums[i-1],nums[i])
dp[i] = tmp_sum
return max(dp)
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): # write code here result=[] i = 0 while i < len(array): list = [] list = array[i:len(array)] result.append(self.maxnum(list)) i = i+1 r = max(result) return r def maxnum(self,list): result = [] sum = 0 for i in range(len(list)): sum = sum + list[i] result.append(sum) r = max(result) return r
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): n = len(array) max1 = array[0] for i in range(n): result = array[i] if max1 < result: max1 = result for j in range(i + 1, n): result += array[j] if max1 < result: max1 = result if result <= 0: break return max1
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): a=array lena=len(a) if lena==0: return 0 if lena==1: return max(a) temp=[] temp.append(max(a)) for i in range(lena-1): for j in range(i+1,lena): if a[j]>0: temp.append(sum(a[i:j+1])) return max(temp) # write code here
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): # write code here l = len(array) if not array: return None cursum = 0 maxsum = array[0] for i in range(0,l): if cursum <= 0: cursum = array[i] else: cursum += array[i] maxsum = max(cursum,maxsum) return maxsum
# -*- coding:utf-8 -*-
class Solution:
def FindGreatestSumOfSubArray(self, array):
(851)# write code here
if not array:
return 0
i = 1
length = len(array)
prev = array[0]
M = -float('inf')
M = prev if prev > M else M
while i < length:
cur = array[i]
if prev > 0:
cur = cur + prev
if cur > M:
M = cur
prev = cur
i += 1
return M
# -*- coding:utf-8 -*-
class Solution:
def FindGreatestSumOfSubArray(self, array):
(851)# write code here
if not array:
return 0
length = len(array)
dp = [0]*length
dp[0] = array[0]
M = -float('inf')
M = M if M > dp[0] else dp[0]
for i in range(1, length):
if dp[i-1] > 0:
dp[i] = dp[i-1]+array[i]
else:
dp[i] = array[i]
if dp[i] > M:
M = dp[i]
return M ### code 2 时间复杂度O(N),空间复杂度O(N)
### code 1 时间复杂度O(N),空间复杂度O(1)
# -*- coding:utf-8 -*- class Solution: def FindGreatestSumOfSubArray(self, array): (851)# write code here def sub_max(item, array): mx = item s = item for i in array: s += i if s > mx: mx = s return mx s_max = array[0] for i in range(len(array)): item = array.pop(0) s_max = sub_max(item,array) if s_max < sub_max(item,array) else s_max return s_max
# -*- coding:utf-8 -*- """ 题目转换成,以第i个元素结尾时,最大的连续子序列是多少 动态规划: res[i] = max(array[i], res[i-1]+array[i]) 也就是说以res[i-1]不大于0时,则不加上之前的序列 """ class Solution: def FindGreatestSumOfSubArray(self, array): if not array: return 0 else: for i in range(1, len(array)): if array[i-1] > 0: array[i] += array[i-1] return max(array) s = Solution() ans = s.FindGreatestSumOfSubArray([2,8,1,5,9]) print(ans)
public class Solution { public int FindGreatestSumOfSubArray(int[] array) { if (array.length==0 || array==null) { return 0; } int curSum=0; int greatestSum=0x80000000; for (int i = 0; i < array.length; i++) { if (curSum<=0) { curSum=array[i]; //记录当前最大值 }else { curSum+=array[i]; //当array[i]为正数时,加上之前的最大值并更新最大值。 } if (curSum>greatestSum) { greatestSum=curSum; } } return greatestSum; } }