问题

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

代码

贪心算法

核心思想就是检查之前 i-1 的元素和,如果小于零就舍弃——对应下面第六行代码

 1 class Solution {
2 public:
3 int maxSubArray(vector<int>& nums) {
4 int cur_nums = 0 , max_nums = INT_MIN;
5 for(int i = 0;i < nums.size();i++){
6 cur_nums = max(nums[i],cur_nums + nums[i]);
7 max_nums = max(cur_nums,max_nums);
8 }
9 return max_nums;
10 }
11 };

动态规划

关于动态转移方程建立的分析如下:

摘自 https://leetcode-cn.com/problems/maximum-subarray/solution/zui-da-zi-xu-he-by-leetcode-solution/

 1 class Solution {
2 public:
3 int maxSubArray(vector<int>& nums) {
4
5 int max_sum = nums[0];
6 vector<int> dp (nums.size());
7 dp[0] = nums[0];
8 for(int i = 1 ;i < dp.size(); i++){
9 dp[i] = max(dp[i-1] + nums[i], nums[i]);
10 max_sum = max(max_sum,dp[i]);
11 }
12 return max_sum;
13 }
14 };

上面时间复杂度为O(n),空间复杂度为O(n)   但是这题从动态转移方程可以看出dp[i] 只依赖于前面 dp[i-1]  。

所以可以不使用数组来保存dp,用int变量来保存。 这样得到优化后的空间复杂度为O(1)的代码如下

 1 class Solution {
2 public:
3 int maxSubArray(vector<int>& nums) {
4
5 int max_sum = nums[0];
6 int dp = nums[0];
7 for(int i = 1 ;i < nums.size(); i++){
8 dp = max(dp + nums[i], nums[i]);
9 max_sum = max(max_sum,dp);
10 }
11 return max_sum;
12 }
13 };

Leetcode53. 最大子序列和的更多相关文章

  1. LeetCode53 最大子序列问题

    题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和.     示例:     输入: [-2,1,-3,4,-1,2,1,-5,4],   ...

  2. LeetCode--53 最大连续子序列(总结)

    # 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. # 示例:# 输入: [-2,1,-3,4,-1,2,1,-5,4],# 输出: 6# 解释 ...

  3. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  4. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  5. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  6. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  7. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  8. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  9. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

随机推荐

  1. oracle DG查看延时时间

    oracle DG查看延时时间 SQL> select value from v$dataguard_stats where name='apply lag'; 例如: SQL> sele ...

  2. MySQL(二):快速理解MySQL数据库索引

    索引 基本概念:索引是在存储引擎层实现的,而不是在服务器层实现的,所以不同存储引擎具有不同的索引类型和实现. 数据结构 Tree 指的是 Balance Tree,也就是平衡树.平衡树是一颗查找树,并 ...

  3. IphoneX适配正确姿势

    IphoneX适配正确姿势 写在前面 距离18年9月iphonex发布以来已经快两年了(所以对于iphonex机型的头部刘海(sensor housing)和底部小黑条(Home Indicator) ...

  4. openstack高可用集群17-openstack集成Ceph准备

    Openstack集成Ceph准备 Openstack环境中,数据存储可分为临时性存储与永久性存储. 临时性存储:主要由本地文件系统提供,并主要用于nova虚拟机的本地系统与临时数据盘,以及存储gla ...

  5. js深克隆与浅克隆

    定义: 浅克隆: 克隆对象的一层属性, 如果对象还有对象的话,里面的对象没有进行克隆,只是把地址给了别人.也可以理解为只是简单的克隆了躯体,但是没有得到其灵魂: 深克隆:克隆对象的多层属性,对象里面还 ...

  6. Autofac的基本使用---5、常用配置

    Autofac的基本使用---目录 创建实例方法 参考:http://www.cnblogs.com/manglu/p/4115128.html InstancePerDependency 对每一个依 ...

  7. EF生成模型时Disigner中无信息

    原博文 http://blog.sina.com.cn/s/blog_a1b63a730101ezs4.html 说明 DbContext是对ObjectContext的简化封装.原来的ObjectC ...

  8. 后台查询出来的list结果 在后台查询字典表切换 某些字段的内容

    list=listEFormat(list, "Class_type", "611");//list查询数据库得到的结果Class_type /** * @Ti ...

  9. docker 使用教程1

    1.概念理解 镜像:docker镜像就像一个个模具. 容器:docker容器就是模具翻模出来的东西. 仓库:仓库就是存放模具的地方. 下面通过运行 hello-world 来理解 docker镜像运行 ...

  10. javaScript编写9*9口诀

    学习html+css+javaScript<!DOCTYPE html> <html> <head> <title>chaoba</title&g ...