【leetcode】Maximum Subarray
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
.
More practice:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
class Solution {
public:
int maxSubArray(int A[], int n) { int sum,maxSum;
sum=maxSum=A[];
for(int i=;i<n;i++)
{
if(sum<) sum=;
sum+=A[i]; if(maxSum<sum) maxSum=sum;
}
return maxSum;
}
};
class Solution {
public:
int maxSubArray(int A[], int n) { divideAndConquer(A,,n-);
} int divideAndConquer(int A[],int left,int right)
{ if(left==right) return A[left]; int mid=(left+right)/; int leftMax=divideAndConquer(A,left,mid);
int rightMax=divideAndConquer(A,mid+,right); int midSum1=;
int midMax1=A[mid]; for(int i=mid;i>=left;i--)
{
midSum1+=A[i];
if(midMax1<midSum1) midMax1=midSum1;
} int midSum2=;
int midMax2=A[mid+]; for(int i=mid+;i<=right;i++)
{
midSum2+=A[i];
if(midMax2<midSum2) midMax2=midSum2;
} int midMax=midMax1+midMax2; return max(max(leftMax,rightMax),midMax); }
};
【leetcode】Maximum Subarray的更多相关文章
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 【LeetCode】Maximum Subarray(最大子序和)
这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...
- 【Leetcode】【Medium】Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【数组】Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...
- 【leetcode】Maximum Gap
Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
随机推荐
- MVC缓存OutPutCache学习笔记 (二) 缓存及时化VaryByCustom
<MVC缓存OutPutCache学习笔记 (一) 参数配置> 本篇来介绍如何使用 VaryByCustom参数来实现缓存的及时化.. 根据数据改变来及时使客户端缓存过期并更新.. 首先更 ...
- Java并发编程核心方法与框架-Semaphore的使用
Semaphore中文含义是信号.信号系统,这个类的主要作用就是限制线程并发数量.如果不限制线程并发数量,CPU资源很快就会被耗尽,每个线程执行的任务会相当缓慢,因为CPU要把时间片分配给不同的线程对 ...
- 解决安装VS2013提示“已停止工作”问题
新安装操作系统(win8.1),手动安装各种驱动,安装VS2013,报错,见下图: 原因:显卡驱动问题. 解决办法:卸载intel显卡驱动这碧池.(系统会自动给你适配合适的)
- 【转】 使用maven创建web项目
生成kafka java客户端时,参考的资料!!!文章来源:http://blog.csdn.net/zhshulin/article/details/37921705 目前做的项目使用的是MAVEN ...
- High Frequency Trading (整理中...)
什么是高频交易系统 1 交易指令完全是由电脑发送,对市场数据的响应延时在微秒级2 系统有专用的软硬件组成,研发时需要大量的计算机专家级的工作3 系统的硬件需要放在离交易所主机很近的位置,所谓co-lo ...
- hdu 4960 Another OCD Patient (最短路 解法
http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...
- 在PHP中遍历数据库表中的数据
数据库中的数据: //1.分别将每一行的每一列遍历出来 //mysql_fetch_row()函数在每一次遍历后会将指针向后自动移动一个单位 while($row=mysql_fetch_row($r ...
- MongoDB的索引(四)
创建索引的好处是可以加快查询速度,但是但来的负面影响就是磁盘的开销和降低写入性嫩. 查看评判当前索引构建情况方法: 1. 使用mongostat工具: 查看mongodb运行状态的程序 使用格式:mo ...
- IBM B16光纤交换机ZOON划分方法
一.ZOON的含义及划分原则 Zoon在光纤存储交换机中的功能类似于以太网交换机VLAN的作用,主要是为了在一台交换机划分出多个逻辑区,用于防范不同应用的存储连接发起广播包,提高光纤交换机 ...
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...