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. 【WEB前端系列之CSS】CSS3动画之Tranition

    前言 css中的transition允许css的属性值在一定的时间区间内平滑的过渡.这种效果可以在鼠标点击.获得焦点.被点击或对元素任何改变中触发,并圆滑的以动画效果改变CSS的属性值.语法: tra ...

  2. Elasticsearch学习之深入搜索三 --- best fields策略

    1. 为帖子数据增加content字段 POST /forum/article/_bulk { "} } { "doc" : {"content" : ...

  3. VMware ESXI5.5 Memories limits resolved soluation.

    在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...

  4. jvisualvm连接远程应用终于成功,附踩大坑记录!!(二:jmx方式)

    一.问题概述 参考前一篇: jvisualvm连接远程应用终于成功,附踩大坑记录!!(一:jstatd方式) 这篇主要讲讲jmx方式. 二.启动前设置jmx参数 我这边拿tomcat举例,其余java ...

  5. maven用变量的方法统一管理jar包版本

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. MongoDB安装、CURD操作、使用场景分析总结(1)

    NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL".非关系型的数据存储 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 ...

  7. 【CF700E】Cool Slogans 后缀自动机+线段树合并

    [CF700E]Cool Slogans 题意:给你一个字符串S,求一个最长的字符串序列$s_1,s_2,...,s_k$,满足$\forall s_i$是S的子串,且$s_i$在$s_{i-1}$里 ...

  8. Unity3D 记第一次面试

    事情是发生在2014-03-05 周三下午 在群里面看到上海艺游急聘Unity3D开发工程师,就整理了下简历投了去!直到接到电话通知我去面试才知道 我之前是有投了简历!太忙了 以至于真的忘了,不过那个 ...

  9. Xcode - Your development team, "", does not support the Push Notifications capability.

    1.问题描述: 从git上checkout了别人的一个工程文件,选择team时,Xcode显示如下问题 Your development team, "xxx.xxx.xxx", ...

  10. 7.24python协程(2)和IO模型

    2018-7-24 08:50:29 异步IO模型 epoll  机制  linux 给每个监听对象绑定回调函数,当要读的对象来了时候,回调函数直接被执行,然后通知用户,效率非常高! python无法 ...