给定一个长度为 n 的数组 arr ,返回其中任意连续子数组的最大累加和
题目保证没有全为负数的数据
数据范围:,数组中元素值
要求:时间复杂度为,空间复杂度为
# # max sum of the subarray # @param arr int整型一维数组 the array # @return int整型 # class Solution: def maxsumofSubarray(self , arr ): # write code here total = 0 maxi = arr[0] for i in range(len(arr)): total = total + arr[i] if total <= 0: total = 0 elif total >= maxi: maxi = total return maxi
# # max sum of the subarray # @param arr int整型一维数组 the array # @return int整型 # class Solution: def maxsumofSubarray(self , arr ): # write code here a,b = 0,0 for x in range(1,len(arr)): a = arr[x-1] + arr[x] b = arr[x] arr[x] = a if a>b else b return arr[-1]