[LeetCode] 53. 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.
问题: 给定一个元素有正有负的数组,求最大连续子数组的和。
思路:
设辅助数组 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 解题思路的更多相关文章
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) whic ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode: 53. Maximum Subarray(Easy)
1. 原题链接 https://leetcode.com/problems/maximum-subarray/discuss/ 2. 题目要求 给定一个整型数组,返回其子串之和的最大值 例如,[-2, ...
随机推荐
- DW CS5序列号
先要改host文件,以防止其连接 Adobe 的激活验证服务器 1. 用记事本编辑“C:\Windows\System32\Drivers\etc\”目录下的 hosts 文件, 在末尾加上: 127 ...
- 什么是 better-scroll(转自知乎网 : 黄轶)
什么是 better-scroll better-scroll 是一个移动端滚动的解决方案,它是基于 iscroll 的重写,它和 iscroll 的主要区别在这里.better-scroll 也很强 ...
- PHP中级程序员常见面试题
1).写一个函数,从一个标准url里取出文件的扩展名,需要取出php或.php <?php $a="http://www.test.com.cn:88/abc/de/fg.php?id ...
- day30 进程
推荐两本书:现代操作系统和操作系统原来,学习好python以后再去研究. 并发:任务的切换,保存状态,存在io的是实现空间和时间的 重复利用 操作系统的发展历史: 第一代(1940-1955)手工 ...
- 第七篇:gcc和arm-linux-gcc常用选项
目录 一.gcc和arm-linux-gcc的常用选项 二.从.c文件到可执行文件过程 一.gcc和arm-linux-gcc的常用选项 常用选型 -v 查看gcc编译器的版本,显示gcc执行时的详细 ...
- python三大器之while,if,for循环
一.for循环(遍历循环) 在Python你可能要经常遍历列表的所有元素,对每个元素执行相同的操作;对于包含数字的列表,可能要对每个元素进行相同的计算;在网站中,可能需要显示文章中的每个标题等等.某一 ...
- 利用谷歌翻译网站和Adobe Acrobat翻译英文文档,且鼠标放置后显示英文原文(无字数限制)(18/12/11更新)
软件:Adobe Acrobat 网页:https://translate.google.cn/?tr=f&hl=zh-CN 方法: 第一步:用Adobe Acrobat 打开英文 ...
- 爬取 StackOverFlow 上有关于 Python 的问题
给定起始页面以及爬取页数,要求得到每一个问题的标题.票数.回答数.查看数 stackflow <- function(page){ url <- "http://stackove ...
- 深浅拷贝--python(预习中随手写的。因为当时很无聊。。。)
需要知识准备,pyhton基本常识,python的小数据池概念. 深浅拷贝操作需要模块导入:import copy emmm,对于python中的两种数据类型来说. 1.数字,字符串 2.列表,元祖, ...
- 在vue项目中添加eslint规则
自己配置脚手架时候如何安装eslint语法规则, 第一步安装 官方推荐的安装包如下 eslint eslint-config-standard eslint-plugin-standard eslin ...