[LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如8 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 【软件分析与挖掘】ELBlocker: Predicting blocking bugs with ensemble imbalance learning
摘要: 提出一种方法——ELBlocker,用于自动检测出Blocking Bugs(prevent other bugs from being fixed). 难度在于这些Blocking Bugs仅 ...
- css笔记 - 张鑫旭css课程笔记之 border 篇
border地址 border特性: 能形成BFC但是不能清除浮动.但是bfc也是把子元素的margin包裹进来,但是拿自己的margin穿透没办法的. 边框宽度不支持百分比 透明border可以突破 ...
- android studio 引用远程仓库下载慢(JCenter下载慢)的办法
https://blog.csdn.net/linglingchenchen/article/details/62236723 解决android studio引用远程仓库下载慢(JCenter下载慢 ...
- Ajax提交表单时验证码自动验证 php后端验证码检测
本文通过源码展示如何实现表单提交前,验证码先检测正确性,不正确则不提交表单,更新验证码. 1.前端代码 index.html <!DOCTYPE html> <html> &l ...
- sencha touch 在线实战培训 第一期 第四节
2014.1.4晚上8点开的课 第一节收费课程,还是有几位同学付费了,这些课程也录像了的,以后也会持续销售. 本期培训一共八节,前三堂免费,后面的课程需要付费才可以观看. 本节内容: ...
- PS-CC常用快捷键总结
灵活使用photoshop软件快捷键是学好该软件的基础,ps快捷键对于ps平时操作有很大帮助 熟练掌握ps的快捷键可以为了处理图片节省很多时间.现在笔者将自己平时常用的快捷键总结如下: 移动工具[V] ...
- 【CF886D】Restoration of string 乱搞
[CF886D]Restoration of string 题意:对于给定的一个母串,定义一个字符串是出现频率最多的,当且仅当它在母串中出现的次数最多(可以有多个出现次数最多的,出现的位置可以重叠). ...
- gnuplot生成gif动画
最近有个任务需要生成一个动态变化的图,然后突然发现gnuplot竟然可以生成gif动画,当真是应正了博客Gnuplot surprising的子标题: I always tell myself: &q ...
- 服务器群秒级别文件同步(ssh+SHELL)
1.介绍 \ 2.业务服务器远程更新浏览服务器文件的脚本 #!/bin/bash operate=$ ip=$ conf_file="/var/www/html/test/ip_list&q ...
- thinkphp---Excel导入!
在做项目的时候,很多时候会遇到需要将excel导入到数据库的操作: 需要用到Excel类: 下载地址: https://gitee.com/meiyouzhanghao/excel 位置:Thinkp ...