关闭

leetCode No.53 Maximum Subarray

标签: leetcode
360人阅读 评论(0) 收藏 举报
分类:

题目

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

标签:Array、Dynamic Programming、Divide and Conquer
相似问题: (E) Best Time to Buy and Sell Stock(M) Maximum Product Subarray

题意

在给定的数组中找到相加和最大的子串,子串大小最小为1。

解题思路

从头开始累加,如果当前的和为正数则可以继续累加,若为负数,继续相加结果必然较下一个单独数小。又因为必须是连续子串所以要抛弃之前的和,从当前值从新开始。

代码

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        m_sum = 0
        max_sum = -2147483648
        for i in nums:
            if m_sum >= 0:
                m_sum += i
            else:
                m_sum = i
            if m_sum > max_sum:
                max_sum = m_sum
        return max_sum

相关链接

源代码(github)
原题

0
0
查看评论

Maximum Subarray -- LeetCode

原题链接: http://oj.leetcode.com/problems/maximum-subarray/  这是一道非常经典的动态规划的题目,用到的思路我们在别的动态规划题目中也很常用,以后我们称为”局部最优和全局最优解法“。 基本思路是这样的,在每一步,我们维护两个变量,...
  • linhuanmars
  • linhuanmars
  • 2014-03-20 09:24
  • 22528

LeetCode53. Maximum Subarray 动态规划和分治法

题意:在一个数组中找到一个和最大的子数组。 有两种方法: 动态规划DP 分治法
  • wmdshhz0404
  • wmdshhz0404
  • 2016-09-20 23:26
  • 390

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

Maximum Subarray  Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, giv...
  • xygy8860
  • xygy8860
  • 2015-07-14 13:59
  • 938

Leetcode 644. Maximum Average Subarray II 最大平均区间2 解题报告

这道题目呢,和之前的643类型差不多,但是更难。 首先这道题我看了Solution的解法,反正不是n2的复杂度,是 n*log(max-min) 的样子吧,一开始怎么做都不对,超时。。。直到直接跑了solution的代码(java),发现原因不是代码写错了,而是python特别慢!!!! 所以不是...
  • MebiuW
  • MebiuW
  • 2017-07-27 22:41
  • 2205

LeetCode(53)Maximum Subarray

题目如下: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4...
  • feliciafay
  • feliciafay
  • 2014-01-29 11:00
  • 5203

【leetcode】【53】Maximum Subarray

一、问题描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array&#...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016-03-01 11:21
  • 356

[leetcode]Maximum Subarray (C语言)

问题描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−...
  • zdavb
  • zdavb
  • 2015-07-05 23:41
  • 515

leetcode -- Maximum Subarray -- 经典问题常考

https://leetcode.com/problems/maximum-subarray/参考http://fisherlei.blogspot.hk/2012/12/leetcode-maximum-subarray.html思路1:假设A(0, i)区间存在k,使得[k, i]区间是以i结尾...
  • xyqzki
  • xyqzki
  • 2015-12-11 23:49
  • 458

LeetCode Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积 题目分析:求一个数组,连续子数组的最大乘积。
  • worldwindjp
  • worldwindjp
  • 2014-10-06 12:09
  • 13251

LeetCode 53. Maximum Subarray Java

53. Maximum Subarray   Find the contiguous subarray within an array (containing at least one number) which has the largest su...
  • DuanLiuchang
  • DuanLiuchang
  • 2016-08-11 10:07
  • 965
    个人资料
    • 访问:15290次
    • 积分:482
    • 等级:
    • 排名:千里之外
    • 原创:34篇
    • 转载:0篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论