美文网首页
Largest SubArray Sum

Largest SubArray Sum

作者: GakkiLove | 来源:发表于2018-05-27 02:52 被阅读0次

Given an unsorted integer array, find the subarray that has the greatest sum. Return the sum.

Assumptions

The given array is not null and has length of at least 1.

Examples

{2, -1, 4, -2, 1}, the largest subarray sum is 2 + (-1) + 4 = 5

{-2, -1, -3}, the largest subarray sum is -1

class Solution(object):
  def largestSum(self, array):
    cur_sum = max_sum = array[0]
    for num in array[1:]:
      cur_sum = max(cur_sum + num,num)
      max_sum = max(cur_sum,max_sum)
    return max_sum

相关文章

网友评论

      本文标题:Largest SubArray Sum

      本文链接:https://www.haomeiwen.com/subject/vugvjftx.html