Python3解leetcode Maximum Subarray
问题描述:
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. 思路:
动态规划问题,从头开始遍历数组,每次都考虑以 当前索引为结束位置 的所有数组求和的最大值,依次类推,从而依次得到以 每一个索引为结束位置的 相应所有数组求和最大值,进而形成一个列表,取该列表最大值即可
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
dp = [0 for i in (range(len(nums)+1))]
dp[0] = float("-inf")
for i in range(1,len(nums)+1):
dp[i] = max(dp[i-1]+nums[i-1],nums[i-1])
return max(dp)
Python3解leetcode Maximum Subarray的更多相关文章
- Python3解leetcode Maximum SubarrayHouse Robber
问题描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...
- Python3解leetcode Maximum SubarrayClimbing Stairs
问题: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- LeetCode: Maximum Subarray 解题报告
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- [LeetCode]Maximum Subarray题解
Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode]Maximum Subarray @ Python
原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...
- LeetCode——Maximum Subarray
Description: Find the contiguous subarray within an array (containing at least one number) which has ...
随机推荐
- CentOS6下基于Nginx搭建mp4/flv流媒体服务器
CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 1.先添加几个RPM下载源 1.1)安装RPMforge的CentOS6源 [roo ...
- 基于imgAreaSelect的用户图像截取
前言:想到用户资料中一般有个图像自我截取的部分,为什么要截取呢,因为好看了.so,经过我各种百度,各种参考,终于打工搞成了,写下纪念纪念,让以后拿来就用也好. 一:想前端ui这东西,我就懒得说话了,哎 ...
- Android之——卸载应用程序
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47357729 不多说,不废话,直接上代码,大家都懂得 //卸载应用程序 //參数为 ...
- iOS项目 -- 模仿花椒直播做的第一层次界面
公最近直播比较火爆,我也跟跟风,自己做一个直播app, 现在打算用金山云直播的,但是去注册的时候,联系那边的工作人员,他们讲使用金山云直播要有公司和他们线下签约才能授权开放直播平台. 怎么办呢?于是我 ...
- 【24题】P2766最长不下降子序列问题
网络流二十四题 网络流是个好东西,希望我也会. 网络流?\(orz\ zsy!!!!!\) P2766 最长不下降子序列问题 考虑我们是如何\(dp\)这个\(LIS\)的. 我们是倒着推,设置\(d ...
- 我的Android进阶之旅------>Android使用正则表达式匹配扫描指定目录下的所有媒体文件(音乐、图像、视频文件)
今天使用正则表达式匹配指定目录下的所有媒体文件,下面将这份代码简化了,可以收藏下来,当作工具类. package match; import java.io.File; import java.uti ...
- Machine Learning in Action(4) Logistic Regression
从这节算是开始进入“正规”的机器学习了吧,之所以“正规”因为它开始要建立价值函数(cost function),接着优化价值函数求出权重,然后测试验证.这整套的流程是机器学习必经环节.今天要学习的话题 ...
- 微信公众号验证TOKEN
服务端验证微信token header('Content-type:text'); define("TOKEN", "weixin"); $signature ...
- 如何浏览github上所有的公开的项目?
github 上面项目多如牛毛,没有维护的.没有意义的或太过偏门的项目也是数不胜数,所以直接按照字母或者更新顺序浏览实在没什么意义. 有一个做法是去 github 搜 awesome list,比如通 ...
- Android中m、mm、mmm、mma、mmma的区别
m:编译整个安卓系统 makes from the top of the tree mm:编译当前目录下的模块,当前目录下需要有Android.mk这个makefile文件,否则就往上找最近的Andr ...