[LeetCode系列]最大连续子列递归求解分析
本文部分参考Discuss: LeetCode.
步骤1. 选择数组的中间元素. 最大子序列有两种可能: 包含此元素/不包含.
步骤2.
步骤2.1 如果最大子序列不包含中间元素, 就对左右子序列进行步骤1.
步骤2.2 如果最大子序列包含, 则结果很简单, 就是左子列的最大后缀子列(即包含左子列最后一个元素--中间元素)加上右子列的最大前缀子列(即包含右子列第一个元素--中间元素)
步骤3. 返回三者中的最大值(左子列最大值, 右子列最大值, 二者拼接最大值).
class Solution {
public:
int maxSubArray(int A[], int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(n==) return ;
return maxSubArrayHelperFunction(A,,n-);
}
int maxSubArrayHelperFunction(int A[], int left, int right) {
if(right == left) return A[left];
int middle = (left+right)/;
int leftans = maxSubArrayHelperFunction(A, left, middle);
int rightans = maxSubArrayHelperFunction(A, middle+, right);
int leftmax = A[middle];
int rightmax = A[middle+];
int temp = ;
for(int i=middle;i>=left;i--) {
temp += A[i];
if(temp > leftmax) leftmax = temp;
}
temp = ;
for(int i=middle+;i<=right;i++) {
temp += A[i];
if(temp > rightmax) rightmax = temp;
}
return max(max(leftans, rightans),leftmax+rightmax);
}
};
[LeetCode系列]最大连续子列递归求解分析的更多相关文章
- UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]
题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b] ...
- hdoj 1231 最大连续子列和
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- [LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析
本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵 ...
- leetcode最短无序连续子数组
平民解法: 既然是找最小数组,那就得到一个排序好的数组,然后直接和初试数组比对,用一个left,right分别记录从最初开始不同,到最后不同的小标,最后左右做差再加一,就能得到长度. 其他解法: 双指 ...
- LeetCode 最短无序连续子数组
题目链接:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/ 题目大意: 略. 分析: 如果排序区间为 [L ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
随机推荐
- hdu4305生成树计数
先预处理出距离,然后判断是否可行,要注意判断是否在一条直线上时判断是在两侧还是一边(wa了四次) double型数据 #include<map> #include<set> # ...
- Zeratul的完美区间(线段树||RMQ模板题)
原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...
- cacti安装和使用
关闭selinux 1.搭建lamp环境 配置apache [root@cacti-server ~]# yum -y install httpd [root@cacti-server ~]# sys ...
- 同一主机,不同域名绑定不同网站(IIS主机头实现方法)
说明: 网站A域名:www.weba.com 网站A路径:D:\website\weba 网站B域名:www.webb.com 网站B路径:D:\website\webb 公用主机IP:111.222 ...
- 【nyoj-1233】差值
描述 输入一个整数数组,将它们连接起来排成一个数,找出能排出的所有数字中最大,最小的两个,输出两个数的差值.例如输入数组{1, 2},则输出9. 输入 第一行输入一个整数T,表示有T组测试数 ...
- L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- 怎样安装 OpenJDK 8 in Ubuntu 14.04 & 12.04 LTS
OpenJDK Java 8 has been made into official Ubuntu repositories for 14.10 Utopic and higher. For Ubun ...
- ISO 18000-6c 访问标签--应用程序访问操作ISO 18000-6C标签的方法
标签访问操作包含三部分: 1.标签选择.某些场景,应用程序只对特定的标签进行操作,Indy Host Library可以指定标签选择条件. 2.执行盘存操作.上报单化后标签EPC区的数据. 3.标签单 ...
- ubuntu16 chrome install
1,download chrome.deb from : https://www.google.com/chrome/index.html 2,double click chrome.deb and ...
- [AOP] 7. 一些自定义的Aspect - Circuit Breaker
Circuit Breaker(断路器)模式 关于断路器模式是在微服务架构/远程调用环境下经常被使用到的一个模式.它的作用一言以蔽之就是提高系统的可用性,在出现的问题通过服务降级的手段来保证系统的整体 ...