leetcode解题报告(12):Maximum Subarray
描述
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的更多相关文章
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- leetCode解题报告5道题(六)
题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest s ...
- 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 ...
随机推荐
- java运算符那些事
&& 逻辑与 &&先运算&&左边的算式,如果为假,则直接停止,后面不管有多少运算式都不再运算,如果为真则继续判断后面的式子,只有所有的条件全部成立,才会 ...
- Swarm系列7--存储介绍
存储介绍 1. 存储使用 与docker一样,在使用swarm服务级别的时候可以定义服务的存储需求, docker存储介绍参考: Docker之应用数据管理(volume/bind mount/tmp ...
- Educational Codeforces Round 65 (Rated for Div. 2)
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- Bminer
Bminer https://www.bminer.me/zh/ Bminer: When Crypto-mining Made Fast¶ Bminer是一款为NVIDIA和AMD GPU深度优化的 ...
- 基于【 Docker】二 || Docker的入门安装
一.前提条件 本文介绍 Docker CE 的安装使用. 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3. ...
- CentOS7使用yum安装PostgreSQL和PostGIS
更新yum源 CentOS7默认yum源的PostgreSQL版本过低,不适合在本版本上使用.在https://yum.postgresql.org/repopackages.php上找到适合Cent ...
- 5.创建执行线程的方式之三 :实现Callable 接口
Callable 接口 一.Java 5.0 在 java.util.concurrent 提供了 一个新的创建执行线程的方式(之前有继承Thread 和 实现Runnable):Callable 接 ...
- Java之IO学习
知其然也要知其所以然,最近对IO流莫名的感觉时候充充电了,所以上网拜读了大神的文章.在学习过程中我找到了一篇很系统很详细的文章贴出来. 声明一下这是转载的文章 原文:https://www.cnbl ...
- DRDA
在谈到分布式DB2数据时,有必要谈谈DRDA. DRDA代表分布式关系数据库体系结构. 它是由IBM开发的一种体系结构,它使关系数据能够在多个平台之间分布. 平台和平台都可以相互通信. 例如,一个DB ...
- NORDIC BLE MAC ADDR
一个ble设备,地址可以分成2大类 1.Public Device Address(公共设备地址) 公共设备地址 Public Device Address是48bits的数字,就和电脑mac地址 ...