问题描述:

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的更多相关文章

  1. Python3解leetcode Maximum SubarrayHouse Robber

    问题描述: You are a professional robber planning to rob houses along a street. Each house has a certain ...

  2. 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 ...

  3. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  4. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. [LeetCode]Maximum Subarray题解

    Maximum Subarray: Find the contiguous subarray within an array (containing at least one number) whic ...

  6. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  7. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  8. [leetcode]Maximum Subarray @ Python

    原题地址:https://oj.leetcode.com/problems/maximum-subarray/ 题意: Find the contiguous subarray within an a ...

  9. LeetCode——Maximum Subarray

    Description: Find the contiguous subarray within an array (containing at least one number) which has ...

随机推荐

  1. SQL Server 的collate的含义

    什么叫排序规则呢?MS是这样描述的:"在 Microsoft SQL Server 2000 中,字符串的物理存储由排序规则控制.排序规则指定表示每个字符的位模式以及存储和比较字符所使用的规 ...

  2. spring4 maven3 mybatis

    1 新建maven工程 http://www.cnblogs.com/quanyongan/archive/2013/04/21/3033838.html 如果在第三步中出现错误,比如类似: Coul ...

  3. android自己定义TextView

    Android控件中的TextView控件仅仅有一个输入框.可是为了用于的操作方便我们应该实现一些功能: 1. 能够直接将内容删除的功能button 2. 可以记录用户曾经输入的数据,同一时候可以将数 ...

  4. EasyPlayerPro(Windows)流媒体播放器功能介绍及应用场景

    EasyPLyerPro(Windows)经过为期一个月的开发已经基本完成,虽然目前仍存在一些小问题,但是总体功能还是趋于比较稳定和强大的,下面对其功能和应用场景做简要介绍. 一.EasyPlayer ...

  5. iOS 打包Framework包含其他Framework的问题

    当你打包一个framework静态库包含另一个第三方静态库时,在工程中使用自己打包的framework,这时编译会出现报错,报错的大概原因是没有找到你打包framework里面的第三方framewor ...

  6. mapreduce的一个模版

    import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import j ...

  7. keras: 在构建LSTM模型时,使用变长序列的方法

    众所周知,LSTM的一大优势就是其能够处理变长序列.而在使用keras搭建模型时,如果直接使用LSTM层作为网络输入的第一层,需要指定输入的大小.如果需要使用变长序列,那么,只需要在LSTM层前加一个 ...

  8. jQuery 的文档操作

    在 js 中也有DOM操作,也可以进行 增删改查 ,但是通过 js 的DOM操作会发现,我的天哪,代码好多.但是 jQuery的文档操作就少了很多. js 中 DOM 的示例 : var box = ...

  9. P3968 [TJOI2014]电源插排

    P3968 [TJOI2014]电源插排 线段树维护最长空区间及左端点位置,这个和$nlongn$的动态最大子序和差不多,就不多解释了 $n$较大哈希优化空间 My complete code: #i ...

  10. uboot 2013.01 s3c6400编译失败

    通常我们对s3c6410平台开发u-boot是在s3c6400的基础上修改而成的,但是从uboot 2013.01这个版本之后的版本都把smdk6400对应的配置给删除了. 这是因为该版本smdk64 ...