LintCode Q44 Minimum Subarray in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年12月15日
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
if nums is None or len(nums) == 0:
return None
c_sum = nums[0] # current sum
s_sum = nums[0] # smallest sum
for i in xrange(1, len(nums)):
if c_sum >= 0:
c_sum = min(c_sum, nums[i])
s_sum = min(s_sum, c_sum)
elif c_sum + nums[i] < 0:
c_sum = c_sum + nums[i]
s_sum = min(s_sum, c_sum)
else:
c_sum = nums[i]
return s_sum
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。