Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6. 这个题目思路跟[LeetCode] 198. House Robber _Easy tag: Dynamic Programming很像, 我们只需要得到动态方程式, A[i] 是maxsum which contains nums[i] for sure,
then A[i] = max(A[i-1] + nums[i], nums[i]), init: A[0] = nums[0] 1. Constraints
1) size >= 1
2) elsement will be integer 2. Ideas Dynamic Programming T: O(n) S; O(1) using rolling array 3. Code
3.1) S: O(n)
class Solution:
def maxSum(self, nums):
n = len(nums)
dp = [] * n
dp[], ans = nums[], nums[]
for i in range(, n):
dp[i] = max(dp[i-] + nums[i], nums[i])
ans = max(ans, dp[i])
return ans

3.2)   S; O(1)   using rolling array

class Solution:
def maxSum(self, nums):
n = len(nums)
dp = []*
dp[], ans = nums[], nums[]
for i in range(, n):
dp[i%] = max(dp[i% -] + nums[i], nums[i])
ans = max(ans, dp[i%])
return ans

4. Test cases

 

[LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

    Given two words word1 and word2, find the minimum number of operations required to convert word1to w ...

  2. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. [LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming

    There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...

  4. [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming

    基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...

  5. [LeetCode] 256. Paint House_Easy tag: Dynamic Programming

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...

  6. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  7. [LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  9. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

随机推荐

  1. hibernate annotation多对多中间表添加其他字段的第三种方法

    本示例主要以学生(T_Student)和课程(T_Course)之间的多对多关系,中间表Score(分数),学生表和课程表是多对多关系,另外为他们的关系添加额外的字段---分数: T_Student类 ...

  2. OpenGL ES 系列教程

    http://www.linuxgraphics.cn/graphics/opengles_tutorial_index.html 本文收集了一套 OpenGL ES 系列教程. www.play3d ...

  3. Android studio 插件安装

    安装插件步骤 一 CodeGlance 最大的用途:可用于快速定位代码.显示在右侧 二 Android Studio Prettify 可以将代码中的字符串写在string.xml文件中 选中字符串鼠 ...

  4. OpenStack入门之【OpenStack-havana】之单网卡-All In One 安装(基于CentOS6.4)

    这篇文章是自己的一篇老文,分享下,请君慢用.... =========================================== [特别申明]:经过了一段时间的不断学习加不断的测试得出本文, ...

  5. 下载Google Play外国区APP技巧

    安卓用户若遇到喜欢的APP是外国区的,只要FQ就能下载.比起果粉还要注册,是简便很多.但有没有更简单的办法?这个必须有!笔者前几天在网上闲逛时,就发现了一个给力的网站.让你不用FQ,只需3个步骤,就能 ...

  6. PCB常见的拓扑结构 (转)

    常见的拓扑结构有: 1.点对点拓扑 point-to-point scheduling     该拓扑结构简单,整个网络的阻抗特性容易控制,时序关系也容易控制,常见于高速双向传输信号线:常在源端加串行 ...

  7. 【CF875F】Royal Questions 最小生成基环树森林

    [CF875F]Royal Questions 题意:国王的n个王子该结婚了!现在从外国来了m位公主,第i位公主的嫁妆是wi.由于进步思想的传播,每个公主在选择配偶的事情上是有自主权的,具体地,每个公 ...

  8. Android Meterial Design Support Library

    extends:http://inthecheesefactory.com/blog/android-design-support-library-codelab At the moment I be ...

  9. 【JAVA】猜数字

    import java.util.*; public class GN { public static void main(String arg[]) { ;// 数字标记 ;// 位置标记 ;// ...

  10. jsp页面获取参数的方法(url解析、el表达式赋值、session取值)【原创】

    最近使用myEclispse做网站,使用jsp+js+css做页面,网站中常用到从列表进入详情页面的跳转,下面对详情页面的值填充方式做一个简单总结: 1.url中使用request获取参数 jsp上方 ...