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. es6 数组扩展方法

    1.扩展运算符 含义: 扩展运算符,三个点(...),将一个数组转为用逗号分隔的参数顺序. 例如: console.log([1,2,3]); console.log(...[1,2,3]);   结 ...

  2. 『C++』Temp_2018_12_13 函数指针

    #include <iostream> #include <string> using namespace std; class Test{ private: string n ...

  3. UDP实现网络通信程序

    VC6.0创建基于UDP协议的网络聊天程序 只有一个工程UDP,服务器和客户端都是这个工程,因为UDP中C/S区分不强化 只讲关键部分,避免累赘 1.为对话框添加控件 2.为控件绑定变量和消息函数 启 ...

  4. Spring Boot 微信-验证服务器有效性【转】

    转:https://blog.csdn.net/jeikerxiao/article/details/68064145 概述 接入微信公众平台开发,开发者需要按照如下步骤完成: 在自己服务器上,开发验 ...

  5. Redis Sentinel 集群安装 step by step

    一. 准备材料 服务器 IP address 操作系统 位数 Redis 版本   CNT06CAH05 192.168.3.47 CentOS 6.5 x64 Redis-3.2.6 sentine ...

  6. 微信小程序 —— 仿制豆瓣(一)

    先预览一下效果 欢迎扫码查看 码云地址:https://gitee.com/mk_23/little_chen_xu.git 预览完成,首先进入app.json文件中配置参数,主要就是配置我们要用的页 ...

  7. Hive(3)-meta store和hdfs详解,以及JDBC连接Hive

    一. Meta Store 使用mysql客户端登录hadoop100的mysql,可以看到库中多了一个metastore 现在尤其要关注这三个表 DBS表,存储的是Hive的数据库 TBLS表,存储 ...

  8. usb之鼠标作为按键输入

    1. 首先搞清楚,鼠标点左键.右键等能得到什么数据,然后分析这些数据上报事件即可. 第一个基本点:usb_alloc_urb函数,创建一个struct urb结构体,只能使用这个函数来创建,它是urb ...

  9. kubernetes资源清单定义

    apiVersion: v1 #必选,版本号,例如v1,版本号必须可以用 kubectl api-versions 查询到 . kind: Pod #必选,Pod metadata: #必选,元数据 ...

  10. Docker入门系列01

    前两篇写了 Docker 如何安装和相关的概念,当然概念的东西省略了很多,主要是自己水平有限,所以后期会可能增添.但以上内容都是用别人的建好的 镜像(Image) ,这怎么行,我们应该自己动手造轮子, ...