[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 ...
随机推荐
- VS 2008 头文件库文件设置
在程序开发中,很多时候需要用到别人开发的工具包,如OpenCV和itk.一般而言,在vs2008中,很少使用源文件,大部分是使用对类进行声明的头文件和封装了类的链接库(静态lib或动态dll). 如果 ...
- mouseleave,mouseout 和mouseover ,mouseenter区别
鼠标离开事件: mouseleave:只有鼠标离开指定元素时才会触发; mouseout 鼠标离开指定元素或内部子元素都会触发; 鼠标在上事件: mouseover:只有鼠标进入指定元素时才会触发; ...
- windows下的zookeeper安装
先在官网下载安装包(https://www.apache.org/dyn/closer.cgi/zookeeper/),单机安装非常简单,只要获取到 Zookeeper 的压缩包并解压到某个目录如:C ...
- 爬虫自动登陆GitHub
import requests from bs4 import BeautifulSoup r1 = requests.get( url='https://github.com/login' ) s1 ...
- C语言清屏函数
Devc++ 与VC中的清屏函数 #include<stdio.h> #include<stdlib.h>//清屏函数的头文 int main() { int i; for(i ...
- 南阳理工大学oj 题目15 括号匹配(二)
括号匹配(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 给你一个字符串,里面只包含"(",")","[&qu ...
- zero-shor learning 数据集
OSR数据集下载地址: http://people.csail.mit.edu/torralba/code/spatialenvelope/ Relative Attributes Marr Priz ...
- HDU - 5961 传递 想法,bfs
题意:给你一个有向图,满足去掉方向是完全图,将其拆成PQ两个图(没有公共边),问你两图是否分别满足对于任意3个点a,b,c 若有一条边从a到b且有一条边从b到c ,则同样有一条边从a到c. 题解:观察 ...
- 自己封装framworks上传到应用商店报错
参考链接: http://www.jianshu.com/p/60ac3ded34a0 http://ikennd.ac/blog/2015/02/stripping-unwanted-archite ...
- Metricbeat 的使用
目标 统计并展示系统的信息 cpu, 内存等 (当然metricbeat能收集的信息种类还很多) 前提 版本: 5.x 已经安装了ELK (elasticsearch, logstash (可选), ...