leetcode-最大子序和(动态规划讲解)
最大子序和(动态规划讲解)
class Solution {
public int maxSubArray(int[] nums) {
if(nums.length==1)return nums[0];
int sum[]=new int [nums.length+1];
sum[0]=0;int temp=0;
for(int i=1;i<nums.length+1;i++){
sum[i]=sum[i-1]+nums[i-1];
}
int len=nums.length;
int max=Integer.MIN_VALUE;
for(int i=0;i<len+1;i++){
for(int j=i+1;j<len+1;j++){
if(sum[j]-sum[i]>max)max=sum[j]-sum[i];
}
}
return max;
}
}
- 令状态dp[i]表示以A[i]作为末尾的连续序列的最大和。比如[-2,1,-3,4,-1,2,1,-5,4] 一个序列,下标分别是0,1,2,3,4,5,6,7,8
- 作如下考虑:因为dp[i]要求是必须以A[i]结尾的连续序列,那么只有两种情况:
class Solution {
public int maxSubArray(int[] nums) {
int dp[]=new int[nums.length+1];
dp[0]=nums[0];
for(int i=1;i<nums.length;i++){
dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
}
int k=0;
for(int i=0;i<nums.length;i++){
if(dp[i]>dp[k])k=i;
}
return dp[k];
}
}
leetcode-最大子序和(动态规划讲解)的更多相关文章
- LeetCode 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...
- Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray)
Leetcode之动态规划(DP)专题-53. 最大子序和(Maximum Subarray) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. ...
- Leetcode题目53.最大子序和(动态规划-简单)
题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连 ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- Leetcode——53.最大子序和
@author: ZZQ @software: PyCharm @file: leetcode53_最大子序和.py @time: 2018/11/26 12:39 要求:给定一个整数数组 nums ...
- 【LeetCode】53.最大子序和
最大子序和 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: ...
- LeetCode 53. 最大子序和(Maximum Subarray)
53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...
- leetcode之53.最大子序和
题目详情 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: ...
- Java实现 LeetCode 53 最大子序和
53. 最大子序和 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 ...
随机推荐
- mysql where 加 多个 或者条件
select * from table where id=1 and uid=2 and (status=2 or status=3 or status=4);
- 转:system.Security.Cryptography C# 加密和解密
以下文转自: http://www.360doc.com/content/13/0122/05/19147_261678471.shtml 总结:注册的时候经过MD5加密存进数据库,在登录的时候需要先 ...
- Objective-C 中的 assign, copy,retain,strong,weak 详解
在IOS开发中,经常会使用 @property(nonatomic,copy)NSString * name; 语句来快速设置set get 方法,在此依次说明atomic .nonatomi ...
- c和c++单链表
c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...
- Spring的jar包不同版本的下载地址
http://repo.spring.io/release/org/springframework/spring/ 可以直接下载不同版本的spring jar包
- Flask入门数据库的查询集与过滤器(十一)
1 查询集 : 指数据查询的集合 原始查询集: 不经过任何过滤返回的结果为原始查询集 数据查询集: 将原始查询集经过条件的筛选最终返回的结果 查询过滤器: 过滤器 功能 cls.query.filte ...
- MAMP:MySQL wasn't able to start
MAMP 我点击start server的时候 发现mysql服务器打不开 http://images.cnblogs.com/cnblogs_com/lwwen/1231721/o_11111111 ...
- TypeScript 运算符
应用场景(待完善) 位运算符 运算符 描述 例子 类似于 结果 十进制 & AND,按位与处理两个长度相同的二进制数,两个相应的二进位都为 1,该位的结果值才为 1,否则为 0. x = 5 ...
- Java三种代理模式
本文转自:https://mp.weixin.qq.com/s/nBmbNP2mR7ei-lDsuOxjWg 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象 ...
- 使用Scala开发Apache Kafka的TOP 20大好用实践
本文作者是一位软件工程师,他对20位开发人员和数据科学家使用Apache Kafka的方式进行了最大限度得深入研究,最终将生产实践环节需要注意的问题总结为本文所列的20条建议. Apache Kafk ...