1.   Maximum Subarray (#53)

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.

简要解析:

本道题主要考查最大连续子序列和的求解方式。

我们可以这样考虑:当我们从头到尾遍历这个数组的时候,对于数组里的一个整数,它有两种选择:1. 加入之前的SubArray;2. 自己另起一个 SubArray。

考虑到有这两种情况,如果之前SubArray的总体和大于0,则认为其对后续结果有益,选择加入之前的SubArray

如果之前SubArray的总体和为0或者小于0,则认为其对后续结果无益,甚至是有害(小于0时),这种情况下选择以这个数字开始,另起一个 SubArray。

设状态为f[j],表示以 S[j] 结尾的最大连续子序列和,则状态转移方程如下:

f[j] = max{f[j −1] + S[j],S[j]}, 其中1 ≤ j ≤ n

target = max{f[j]}, 其中1 ≤ j ≤ n

n  情况一:S[j] 不独立,与前面的某些数组成一个连续子序列,则最大连续子序列和为 f[j −1] + S[j]。

n  情况二:S[j] 独立划分成为一段,即连续子序列仅包含一个数 S[j],则最大连续子序列和为 S[j]。

实现代码:

// LeetcodeTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <climits>
#include <ctime>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cstdlib>
#include <windows.h>
#include <string>
#include <cstring> using namespace std; class Solution {
public:
int maxSubArray(vector<int>& nums) {
int n = nums.size();
int result = nums[0], count=0;
for(int i=0; i<n; i++){
count +=nums[i];
result = max(result,count);
if(count<0)
count=0;
}
return result;
}
}; int main(){
vector<int>nums;
for(int i=0; i<6; i++){
nums.push_back(i);
}
Solution s;
cout<<s.maxSubArray(nums)<<endl;
system("pause");
return 0;
}

  

【leetcode】Maximum Subarray (53)的更多相关文章

  1. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  2. 【LeetCode】Maximum Subarray(最大子序和)

    这道题是LeetCode里的第53道题. 题目描述: 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1 ...

  3. 【Leetcode】【Medium】Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  5. 【Leetcode】Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. 【数组】Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. 【LeetCode】713. Subarray Product Less Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/subarray ...

  8. 【leetcode】Maximum Gap

    Maximum Gap Given an unsorted array, find the maximum difference between the successive elements in ...

  9. 【leetcode】Maximum Gap(hard)★

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

随机推荐

  1. java学习流程

    J2SE 面向对象-封装.继承.多态内存的分析递归集合类.泛型.自动打包与解包.Annotation IO 多线程.线程同步 TCP/UDP AWT.事件模型.匿名类 正则表达式反射机制 2:数据库( ...

  2. Ubuntu下sphinx使用

    Ubuntu安装 参考文档 进入sphinx mysql -h0 -P9306 查询 select * from 索引名

  3. MapReduce实现倒排索引(类似协同过滤)

    一.问题背景 倒排索引其实就是出现次数越多,那么权重越大,不过我国有凤巢....zf为啥不管,总局回应推广是不是广告有争议... eclipse里ctrl+t找接口或者抽象类的实现类,看看都有啥方法, ...

  4. DropDownList控件

    1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...

  5. 有用的git建议

    这篇文章的目的是给经常使用git管理项目提供一个有益的提醒.如果你是git新手,可以先阅读文后的引用部分,然后在回头阅读此篇文章.在介绍git命令之前,你可以先看看来自 on-my-zsh 提供的别名 ...

  6. HD2222 Keywords Search(AC自动机入门题)

    然而还不是很懂=_= #include <iostream> #include <cstring> #include <algorithm> #include &l ...

  7. mybatis 批量更新

    <update id="batchUpdate" parameterType="java.util.List"> <foreach colle ...

  8. flask初探

    为什么我们需要模板 让我们来考虑下我们该如何扩充我们这个小的应用程序. 我们希望我们的微博应用程序的主页上有一个欢迎登录用户的标题,这是这种类型的应用程序的一个"标配".忽略本应用 ...

  9. 在Excel中使用SQL语句查询和筛选

    本文转自:http://blog.sina.com.cn/s/blog_5fc375650102e1g5.html 今天在微博上看到@数据分析精选 分享的一篇文章,是关于<在Excel中使用SQ ...

  10. C++ ## ... 实用

    关于...的使用...在C宏中称为Variadic Macro,也就是变参宏.比如:#define myprintf(templt,...)fprintf(stderr,templt,__VA_ARG ...