描述

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.

分析

这道题一开始没什么思路,看了下网上的解答

https://discuss.leetcode.com/topic/6413/dp-solution-some-thoughts

应该是个动态规划(DP)问题,不过说实话我对DP问题还真没什么想法。。。

但是看了下代码,秒懂(看来就算是DP问题,也是DP中的弱智)。

这道题其实很简单,就是我们每取得一个元素,就把它累加,然后和累加前的值做比较,如果比累加前的值大,就取累加后的值,否则就取累加前的值(好像叫局部最优解)。举个例子:

数组A=[1,2,3,-1,1]

当前最大值为6时(即遍历了前三个元素),下一元素为-1,累加后的值(sum)为5,小于6,则最大值(ans)还是6。再累加下一元素,发现累加后sum为6(5+1),因此不变。

但是还要考虑负数的情况:

数组B=[-2,-1,-3,3,2,1]

可以想象一下,如果是一连串的负数,那么每次累加后会变成更小的负数,那还不如不加,更一般的说:当累加后的值sum<0时,那就把sum置为0.

比如对于上面的数组B,一开始sum的值为0(初始值),累加一次后sum的值为-2,比较一次后ans更新为-2.当进入新的一轮循环时,发现sum<0,就把sum置为0,再让sum和新的元素相加,sum变为-1,比较后ans更新为-1.这样,ans就能保证保存的是更大的负数。当遍历到3时,sum经过累加后就为正数了,这时候就回到了数组A的情况。

代码如下:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ans = INT_MIN;
int sum = 0;
for(int i = 0; i != nums.size(); ++i){
if(sum < 0) sum = 0;
sum += nums[i];
ans = max(sum,ans);
}
return ans;
}
};

leetcode解题报告(12):Maximum Subarray的更多相关文章

  1. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  2. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  3. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  4. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  5. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  6. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  7. leetcode解题报告(15):Third Maximum Number

    描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...

  8. leetCode解题报告5道题(六)

    题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...

  9. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. Redis主从及Cluster区别及注意事项

    https://yq.aliyun.com/articles/647342 https://blog.csdn.net/biren_wang/article/details/78117392 http ...

  2. 小程序文案过长,‘收起/展开’文字,createSelectorQuery 获取节点成功,boundingClientRect 返回信息null

    问题描述: wxml中id是动态生成的. 获取节点信息是在onReady生命周期函数内延时500ms执行的,select(id)可以获取全部节点信息,boundingClientRect (rect) ...

  3. Codeforces 1249 E. By Elevator or Stairs?

    传送门 首先显然下楼的操作一定是不优的,所以只要考虑上楼 设 $f[i]$ 表示到第 $i$ 层时需要的最少时间 那么首先考虑走楼梯,有转移,$f[i]=f[i-1]+a[i-1]$ 然后考虑坐电梯有 ...

  4. 上传文件-layui+ashx

    public void ProcessRequest (HttpContext context) { if (true) { context.Response.ContentType = " ...

  5. C#操作DOS命令,并获取处理返回值

    // /*---------------- // // 文件名:Method // // 文件功能描述: // //    使用 ADB 来进行安卓设备与PC端之间的文件交互,具体adb命令操作请百度 ...

  6. 九、小程序 Redux详解与在小程序中怎么使用(action和reducers)

    什么是Redux ​ Redux我们可以把它理解成一个状态管理器,可以把状态(数据)存在Redux中,以便增.删.改.例如: 从服务器上取一个收藏列表,就可以把取回来的列表数据用Redux管理,多个页 ...

  7. Java后端HttpClient Post提交文件流 及服务端接收文件流

    客户端将文件转换为流发送: 依赖的包: <dependency> <groupId>org.apache.httpcomponents</groupId> < ...

  8. bootstrap 模态框在iphone微信内点击无效

    <a  data-toggle="modal" data-target="#wwww" href=""  οnclick=" ...

  9. Struts2系列漏洞起始篇

    前言 到目前位置struts2的漏洞编号已经到了S2-057,一直想系统的学习下Struts2的漏洞,但由于工作量较大,一直搁浅.最近由于工作需要,借此机会来填下坑.个人认为一个框架漏洞出来了仅仅看下 ...

  10. dubbo框架梳理

    Dubbo分层 Dubbo框架运行主要分如下九层: 配置层:config 服务代理层:proxy 注册中心层:registry 路由层:cluster 监控层:monitor 远程调用层:protoc ...