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. Swift_TableView(delegate,dataSource,prefetchDataSource 详解)

    Swift_TableView(delegate,dataSource,prefetchDataSource 详解) GitHub import UIKit let identifier = &quo ...

  2. 【2017年最新】 iOS面试题及答案

    设计模式是什么? 你知道哪些设计模式,并简要叙述? 设计模式是一种编码经验,就是用比较成熟的逻辑去处理某一种类型的事情. 1). MVC模式:Model View Control,把模型 视图 控制器 ...

  3. shell习题第2题:统计ip访问量

    [题目要求] 有日志1.log,部分内容如下: 112.111.12.248 – [25/Sep/2013:16:08:31 +0800]formula-x.haotui.com “/seccode. ...

  4. 用JQ实现的一个简单轮播

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>lb ...

  5. IO流之字节流

    IO流分类 按照数据流向 输入流:从外界(键盘.网络.文件…)读取数据到内存 输出流:用于将程序中的数据写出到外界(显示器.文件…) 数据源 目的地 交通工具 按照数据类型 字节流:主要用来处理字节或 ...

  6. Hive(8)-常用查询函数

    一. 空字段赋值 1. 函数说明 NVL:给值为NULL的数据赋值,它的格式是NVL( value,default_value).它的功能是如果value为NULL,则NVL函数返回default_v ...

  7. go基础语法-条件语句

    1.if else 语句 if语句后面的条件不需要括号 if n > 0 { return 1 }else { return -1 } 'if'之后,条件判断之前,可以初始化变量(作用域为整个i ...

  8. 2018南京网络赛L题:Magical Girl Haze(最短路分层图)

    题目链接:https://nanti.jisuanke.com/t/31001 解题心得: 一个BZOJ的原题,之前就写过博客了. 原题地址:https://www.lydsy.com/JudgeOn ...

  9. 20154327 Exp3 免杀原理与实践

    实践内容 基础问题回答 (1)杀软是如何检测出恶意代码的? 杀毒软件主要靠特征码进行查杀,匹配到即为病毒. 还有通过云查杀,查看云端库中该文件是否属于恶意代码. 跟踪该程序运行起来是否存在恶意行为,来 ...

  10. 成都优步uber司机第五组奖励政策

    7月14日,成都优步uber团队发布了第五组用户分组.在传言要推出第四组的时候,心想事不过三吧,意外,现在第五组都出来了.一起看看成都优步司机第五组的详细内容!滴滴快车单单2.5倍,注册地址:http ...