[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 ...
随机推荐
- css3中自定义 placeholder 文本颜色
对于 ie 浏览器我们可以通过自定义的 class 名称,直接修改 span 这个标签的样式.对于其他浏览器诸如谷歌和火狐就需要特殊处理了,不多说直接上代码: ::-webkit-input-plac ...
- IOS UI-模态视图
一.简单介绍 除了push之外,还有另外一种控制器的切换方式,那就是Modal 任何控制器都能通过Modal的形式展⽰出来 Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为⽌ ...
- day7-python类反射
一.概述 一般的高阶语言都有反射的功能特性,python也不例外,网上资料显示,python支持类反射和模块反射,今天就先学习一下类反射的相关知识,模块反射后续再展开把.Python的类反射用于把字符 ...
- 011——数组(十一)array_merge array_merge_recursive array_change_key_case
<?php /** */ //array_merge() 将多个数组合并,生成新数组.当键名相同时,后者覆盖前者 /*$array1=array('weburl'=>"bbs.b ...
- SqlServer和MySQL中存储过程out返回值处理C#代码
1.SqlServer中out处理 C#代码 #region"SqlServer中存储过程处理out返回值" //public void getdata() //{ // stri ...
- 普通用户开通sudo权限:xxx is not in the sudoers file.This incident will be reported.的解决方法
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是: chmod u+w /etc/sudoers 3.编辑sudoers文件 vi /etc/s ...
- SVM之解决线性不可分
SVM之问题形式化 SVM之对偶问题 SVM之核函数 >>>SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 上一篇SVM之核函数介绍了通过计算样本核函数,实际上将样本映射 ...
- Miscosoft Visual Studio项目guid取值
There isn't an easy way to change the type of a project in Visual Studio project once it is created; ...
- postgresql与Oracle:空字符串与null
空字符串:两个单引号,中间无空格等任何内容 在postgresql中,空字符串与null是不同的:而oracle中,空字符串与null等同.测试如下: postgresql中: postgres=# ...
- Mac安装三方软件
安装提示“xxx软件已损坏,打不开,您应该将它移到废纸篓”的提示,其实并不是软件本身有问题,而是Mac系统的一个安全机制问题步骤1:Spotlight搜索(快捷键:command+空格或右上角搜索的符 ...