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.

问题: 给定一个元素有正有负的数组,求最大连续子数组的和。

思路:

设辅助数组 v, v[i] 表示以 nums[i] 为右端元素的最大连续子数组的和。v[i], v[i-1] 以及 nums[i] 的关系如下。

v[i] = max( nums[i], nums[i] + v[i-1] )

数组 v 中的最大值,则是整个数组的最大连续子数组的和。

class Solution {
public:
/**
* 求一维数组的最大值元素的值
*
*/
int maxElement(vector<int>& v){ if(v.size() == ){
return ;
} int max = v[];
for (int i = ; i < v.size(); i++) {
if (v[i] > max) {
max = v[i];
}
} return max;
} int maxSubArray(vector<int>& nums) { bool hasPositive = false; vector<int> v(nums.size(), ); if (nums[] >= ) {
hasPositive = true;
v[] = nums[];
}else{
v[] = ;
} for (int i = ; i < nums.size(); i++) {
if (v[i-] + nums[i] >= ) {
hasPositive = true;
v[i] = v[i-] + nums[i];
}
} // print_vector(v); int res;
if (hasPositive == false) {
res = maxElement(nums);
}else{
res = maxElement(v);
} return res;
}
};

本题目是一年前做的,在这里记录下解题思路。补充几点理解:

1. 从上面数组 v 的公式中,可以看出本问题满足 DP 的两个主要性质 overlapping substructure & optimal substructure 。

2. 由于只需要求出最大的连续子数组之和,上面算法可以不用辅助数组,节省空间。有辅助数组,方便查看校对中间结果。

3. 本题的解题思路,也可以理解为是一个滑动窗口算法,通过滑动窗口的左右两端 l 和 r, 求得所有元素分别为右端的最大连续子数组,其中的最大值即为题目的姐。

[LeetCode] 53. Maximum Subarray 解题思路的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  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 (子数组的最大和) 解题思路方法

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

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

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

  9. LeetCode: 53. Maximum Subarray(Easy)

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

随机推荐

  1. webuploader实现上传视频

    之前有人让我做一个webuploader上传视频,但是一直没有时间,现在抽出了时间来.来完成以下这个简单的demo 第一步,上传视频和上传 图片有什么区别么? 其实是没有的,因为执行的操作都是上传,所 ...

  2. svn出现目标计算机积极拒绝无法链接

    这是由于没有启动服务器端监控的原因,只需要执行以下代码即可 svnserve -d --listen-port 8000 -r /opt/svn

  3. IO流,Properties基本功能和遍历

    import java.util.Enumeration; import java.util.Iterator; import java.util.Properties; import java.ut ...

  4. flume搭建新手测试环境

    硬件环境: 腾讯云,两台服务器8G 双核 软件环境: flume1.8.jdk1.8,centos6 第一次搭建也是各种找文件,只知道flume是日志抓取服务,也听说了非常稳定强大的服务,正好公司需要 ...

  5. Python3简单登录接口编写及遇到的问题分享

    1.程序目标 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 2.思路 利用python中的pickle模块,实现用户登录信息(采用dict)和被锁定用户信息(采用list)的存储.所以我预先 ...

  6. Linux3.5—视屏模块学习与分析

    插入USB摄像头后,我看到了识别出的一些信息,在内核源码中搜到了相关信息: 搜索之后,在uvc_driver.c 帮助文档:linux-3.5/Documentation/video4linux/v4 ...

  7. SpaceVim 语言模块 dart

    原文连接: https://spacevim.org/cn/layers/lang/dart/ 模块简介 功能特性 依赖安装及启用模块 启用模块 语法检查及代码格式化 安装 dart-repl 快捷键 ...

  8. Caliburn.Micro 项目文档(翻译):Screens, Conductors and Composition

    原文地址(项目说明文档):[Documentation  Screens, Conductors and Composition]http://caliburnmicro.codeplex.com/w ...

  9. 1030: [JSOI2007]文本生成器

    1030: [JSOI2007]文本生成器 https://www.lydsy.com/JudgeOnline/problem.php?id=1030 分析: AC自动机+dp. 正难则反,求满足的, ...

  10. python简单的socket 服务器和客户端

    服务器端代码 if "__main__" == __name__: try: sock = socket.socket(socket.AF_INET, socket.SOCK_ST ...