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 ...
随机推荐
- Layui连接mysql操作CRUD案例
今天分享的是一个新前端框架Layui,用它来链接数据库实现一下crud的操作. 一:layui简历 layui,是一款采用自身模块规范编写的前端 UI 框架,遵循原生 HTML/CSS/JS 的书写与 ...
- kubernetes核心组件kube-proxy
一. kube-proxy 和 service kube-proxy是Kubernetes的核心组件,部署在每个Node节点上,它是实现Kubernetes Service的通信与负载均衡机制的重 ...
- k8s基础环境搭建
环境准备 服务器之间时间同步 1. 关闭防火墙 systemctl stop firewalld setenforce 0 2. 设置yum源 三台机器都要设置一个master两个node节点 下 ...
- redis的下载和安装
下载 http://download.redis.io 这里我们以redis的5.0.5版本和centos7环境为基础介绍 安装 1.将下载的redis-5.0.5.tar.gz文件上传到linux上 ...
- 关闭google默认打开翻译提醒
关闭google默认打开翻译提醒 在header中添加以下代码: <meta name="google" content="notranslate" /& ...
- CentOS7使用yum安装PostgreSQL和PostGIS
更新yum源 CentOS7默认yum源的PostgreSQL版本过低,不适合在本版本上使用.在https://yum.postgresql.org/repopackages.php上找到适合Cent ...
- 数据库之sqlite
数据创建数据 CREATE TABLE IF NOT EXISTS ArpAudit (ID INTEGER PRIMARY KEY autoincrement NOT NULL, UserName ...
- Rocketmq 集群部署
10.1.0.178 配置文件 broker-a-m.properties brokerClusterName=PaymentClusterbrokerName=broker-anamesrvAddr ...
- 在 Queue 中 poll()和 remove()有什么区别?(未完成)
在 Queue 中 poll()和 remove()有什么区别?(未完成)
- flask例子
1.例子: https://blog.csdn.net/z564359805/article/details/83474387 def get_embedding(bc,query_list,ba ...