def minSubArray(nums):
max = min = sum(nums)
maxList = minList = nums
n = len(nums)+1
for i in range(n+1):
for j in range(i+1,n+1):
sonList = nums[i:j]
Sum = sum(sonList)
if Sum >= max:
max = Sum
maxList = sonList
elif Sum < min:
min = Sum
minList = sonList
print maxList,sum(maxList)
print minList, sum(minList)
class Solution:
"""
@param nums: A list of integers
@return: An integer denote the sum of maximum subarray
"""
def maxSubArray(self, nums):
# write your code here
n = len(nums)
maxSum = sum(nums)
curSum = 0
for i in range(n):
# 从i开始求和,如果当前和大于maxSum,则赋值给maxSum
curSum += nums[i]
if curSum > maxSum:
maxSum = curSum
# 前面的和如果已经小于0了,那么加上下一个元素值,肯定是小于下一个元素值
# 所以如果前面加起来的值小于0了,则舍弃前面的和,从下一位开始继续求和
if curSum < 0:
curSum = 0
return maxSum
class Solution:
"""
@param nums: a list of integers
@return: A integer denote the sum of minimum subarray
"""
def minSubArray(self, nums):
# write your code here
n = len(nums)
minSum = sum(nums)
curSum = 0
for i in range(n):
# 从i开始求和,如果当前和小于于minSum,则赋值给minSum
curSum += nums[i]
if curSum < minSum:
minSum = curSum
# 前面的和如果已经大于0了,那么加上下一个元素值,肯定是大于下一个元素值
# 所以如果前面加起来的值大于0了,则舍弃前面的和,从下一位开始继续求和
if curSum > 0:
curSum = 0
return minSum