leetcode - 53. Maximum Subarray - Easy

descrition

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

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],

the contiguous subarray [4,-1,2,1] has the largest sum = 6.

解析

这是算法导论上的经典例题,用于介绍“分而治之”思想的。

这里可以达到的最优解是: 时间-O(n),空间-O(1)。核心思想是动态规划,使用 dp[i] 表示截止 nums[i] 的连续最大和,dp[i] = max( dp[i-1] + nums[i], nums[i] ),而最大和就是 dp[0,...,n-1] 中的最大值。基于观察发现 dp[i] 的计算只依赖于前一步的结果,因此可以进一步减小空间复杂度,迭代时的状态变换:curSum = max(curSum+nums[i], nums[i]),同时用 maxSum 存储当前的最大值,即 maxSum = max(maxSum, curSum)。

具体实现参考代码。对“分而治之”思想感兴趣的可以参考「算法导论」。

code

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits> using namespace std; class Solution{
public:
int maxSubArray(vector<int>& nums){
int curSum = 0;
int maxSum = numeric_limits<int>::min();
for(int n : nums){
if(curSum < 0){
curSum = n;
}else{
curSum += n;
} maxSum = max(maxSum, curSum);
} return maxSum;
}
}; int main()
{
return 0;
}

[array] leetcode - 53. Maximum Subarray - Easy的更多相关文章

  1. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. 41. leetcode 53. Maximum Subarray

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

  4. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  5. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  6. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  7. LeetCode 53. Maximum Subarray(最大的子数组)

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

  8. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

  9. LeetCode: 53. Maximum Subarray(Easy)

    1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2, ...

随机推荐

  1. react 体验 react与vue的比较

    用了 vue 大半年了,不过我在2016年暑假的时候就看到了 react 这个项目,有点想学习一番,之前学习的都是基础语法和一些基础用法吧,总的来说 mvvm 框架确实都很相似,会一个就可以了; 今天 ...

  2. oracle 物化视图 job

    在oracle数据库里边,创建物化视图之后,系统在DBMS_jobs文件夹下,会自动创建相应的job,右键执行job的时候报错如下: 问题:ORA-12012: 自动执行作业 198 出错 ORA-1 ...

  3. 微软云计算 Massive Data 处理语言Scope 1

    Massive Data处理一直是云计算中很重要的一个环节.目前像Google,Yahoo在相关方面都有自己专有的技术.例如Google的基于MapReduce的Sawzall语言.和Yahoo基于H ...

  4. Mysql实现企业级日志管理、备份与恢复实战

    背景 随着业务的发展,公司业务和规模不断扩大,网站积累了大量的用户信息和数据,对于一家互联网公司来说,用户和业务数据是根基.一旦公司的数据错乱或者丢失,对于互联网公司而言就等于说是灭顶之灾,为防止系统 ...

  5. Linux压力测试软件Stress安装及使用指南

      一.Stress是什么 stress是一个linux下的压力测试工具,专门为那些想要测试自己的系统,完全高负荷和监督这些设备运行的用户. 二.安装 将stress的安装包上传并解压到linux服务 ...

  6. [Android App]IFCTT,即:If Copy Then That,是一个基于IFTTT的"This"实现

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/8075738.html IFCTT,即:If Copy Then ...

  7. github搭建个人博客 hexo框架 next主题

    之前就想搭建自己的博客,现在终于得以实施. 参考了多篇博客,然后又在虚拟机了往往复复试了好多次, 现在基本搭建完毕. 我是基于hexo博客框架, next主题搭建的博客, 静态网页.   大体就是, ...

  8. memcached subList序列化问题

    某个业务场景需要将list超过10个元素时截取前10个然后再丢memcached缓存,因此写了这么一行代码 if(list.size()>10){ list=list.subList(0, 10 ...

  9. Material04 MdListModule模块

    1 在共享模块中导入MdListModule模块 import { NgModule } from '@angular/core'; import { CommonModule } from '@an ...

  10. [每天一个Linux小技巧] 强制让内核按单核模式启动

    在启动參数里追加 nosmp nosmp的说明例如以下: nosmp [SMP] Tells an SMP kernel to act as a UP kernel, and disable the ...