【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 ...
随机推荐
- 集合 ArrayList
/* * 功能:演示java集合的用法:ArrayList */ package com.jihe; //先引入一个包 import java.util.ArrayList; public class ...
- Last-Modified、ETag、Expires和Cache-Control
前言 在客户端通过浏览器发出第一次请求某一个URL时,根据 HTTP 协议的规定,浏览器会向服务器传送报头(Http Request Header),服务器端响应同时记录相关属性标记(Http Rep ...
- jQuery EasyUI API 中文文档 - ValidateBox验证框
jQuery EasyUI API 中文文档 - ValidateBox验证框,使用jQuery EasyUI的朋友可以参考下. 用 $.fn.validatebox.defaults 重写了 d ...
- iOS- storyboard this class is not key value coding-compliant for the key xxx
如图: 在使用storyboard的时候出现此问题,主要是因为给storybroad中的view拖线的时候,有时不小心线拖错了,或者再次拖线导致代码中控件的名字与之前拖线时定义的名字不同导致的. 解决 ...
- [Html5]sessionStorage和localStorage的区别
摘要 有时需要在浏览器中保存一些数据,特别在app中嵌入的h5页面中,需要在webview中保存一些数据,作为客户端的数据持久化. h5中web storage有两种存储方式:sessionStora ...
- asp.net core csrf
如果用tag 比如 <form asp-action="Login" asp-controller="Account" method="post ...
- 在nodejs下express 从安装到运行的全过程
安装过程: npm install -gd express npm install -g express-generator express -V //查看版本,现在一般都是4.x系列的 expr ...
- 淘宝首页源码藏美女彩蛋(下)(UED新作2013egg)
我们已经知道,执行美女会得到"彩蛋",而正是彩蛋做到了taobaoUED展现给大家的神奇的前端魅力.今天我们来看看FP.egg&&FP.egg("%cjo ...
- Apache中,同一IP使用多域名对应多个网站的方法
首先dns中确定有相应的A记录, abc IN A 211.154.2.5 mail IN A 211.154.2.5 这个讲的是在windows下面配置apache虚拟主机: 一.配置虚拟 ...
- C#中的抽象方法和虚方法有什么区别?
抽象方法是只有定义.没有实际方法体的函数,它只能在抽象函数中出现,并且在子类中必须重写:虚方法则有自己的函数体,已经提供了函数实现,但是允许在子类中重写或覆盖.重写的子类虚函数就是被覆盖了.