确定状态

这道题难就在确定状态。因为根据给定的输入,我们可能在中间找到最大值,而不是在最后找到最大值。

但是我们可以用一个变量去保存全局的最大值

dp[i]表示从下标0至下标i最大的子序列和

状态转移方程

dp[i] = max(dp[i-1]+nums[i],nums[i])\

初始条件和边界条件

dp[0] = =nums[0]

计算的顺序

dp[0] ..... dp[n-1]

代码

class Solution {public int maxSubArray(int[] nums) {int n = nums.length;int[] dp = new int[n];//dp[i] means the maximum subarray ending with A[i];dp[0] = nums[0];int max = dp[0];for(int i = 1; i < n; i++){dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);max = Math.max(max, dp[i]);}return max;}
}

动态规划实战8 leetcode-53. Maximum Subarray相关推荐

  1. 【动态规划】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray 原题描述(求子序列最大和/最大子串):Find the contiguous subarray within an array (conta ...

  2. 【DP】LeetCode 53. Maximum Subarray

    LeetCode 53. Maximum Subarray Solution1:我的答案 动态规划 class Solution { public:int maxSubArray(vector< ...

  3. [Lintcode]41. Maximum Subarray/[Leetcode]53. Maximum Subarray

    41. Maximum Subarray/53. Maximum Subarray 本题难度: Eas Topic: Dynamic Programming Description Given an ...

  4. LeetCode(53):Maximum Subarray

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

  5. [LeetCode]: 53: Maximum Subarray

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

  6. leetCode 53. maximum subarray

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

  7. LeetCode#53 Maximum Subarray

    Problem Difinition: Find the contiguous subarray within an array (containing at least one number) wh ...

  8. C#解leetcode 53.Maximum Subarray

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

  9. 53.Maximum Subarray

    /** 53.Maximum Subarray * 2016-5-7 by Mingyang * 如果我们从头遍历这个数组.对于数组中的其中一个元素,它只有两个选择: 1.* 要么加入之前的数组加和之 ...

  10. LeetCode 53. Maximum Subarray--动态规划--C++,Python解法

    题目地址:Maximum Subarray - LeetCode Given an integer array nums, find the contiguous subarray (containi ...

最新文章

  1. 野人与传教士过河java_传教士和野人过河(经典MC问题)
  2. python 多继承的问题
  3. 武汉大学计算机学院的李明,李明副研究员
  4. QT 简单应用之播放WAV声音
  5. 仿苹果手机_安卓变苹果,苹果控制中心也能用上了
  6. 2019年一线城市程序员工资大调查
  7. 计算机 电脑 整机 加密,如何加密文件夹
  8. deepin关机卡桌面_有了MyDock,我也有了一个MacBook桌面
  9. 家庭软路由方案:3865U + ESXi6.7(OpenWRTiKuai) + 花生壳蒲公英P5 + 领势MX5300 + 群辉1621 做到国内国外分流、内网设备流控、内网穿透、异地组网
  10. python中del的用法
  11. android 短信类型,短信分类软件(短信夹)下载-短信分类(短信夹)安卓版下载 - Iefans...
  12. sinc函数卷积_从采样点到声音:sinc函数和卷积
  13. 2019湖北省赛网络赛C Store CDQ分治
  14. attempting to bokeyaunrun eclipse useing the jre instead of jdk,to run eclipse using
  15. 一种高性能计算机网络控制技术,一种高性能流量计算机的研制
  16. 动手学Android之六——布局初步(三)
  17. 字节流读写文件案例——模拟文件(头像)上传功能
  18. python中什么叫类、什么叫对象_Python中的类和对象是什么
  19. 大数据面试杀招 | Flink,大数据时代的“王者”
  20. php计算用户留存,利用Python计算新增用户留存率

热门文章

  1. 学习总结 java 父子级
  2. 【POJ】【2699】The Maximum Number of Strong Kings
  3. javascript设计模式——Module
  4. 解决Linux下SSH等终端乱码问题
  5. C# asp.net mvc,做 301 永久重定向!
  6. PHP生成HTML的技术原理
  7. windows——JDK下载与安装及环境变量配置
  8. jQuery操作input值总结
  9. erlang随机数问题
  10. SQL server 数据导入导出BCP工具使用详解