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. 表格不被内容撑大,且超出的内容变为省略号(css)

    今天写代码,发现表格会被内容撑大,影响到了网页的整体布局. 百度了一解决方法,下面是代码和我的备注 table{table-layout: fixed;}        //固定表格 table td ...

  2. 5G为何采纳华为力挺的Polar码?一个通信工程师的大实话

    Polar码被采纳为5G eMBB场景的控制信道编码,这两天连续被这条消息刷屏,连吃瓜群众都直呼好爽. 然而,随着媒体报道的持续发酵,真相在口口相传中变了形,不乏夸大不实之嫌,小编终于坐不住了,也想吐 ...

  3. 正则去掉img标签的style样式

    $body = '<div style="width:100px; height:20px;"><img alt="test" src=&qu ...

  4. PHP文件大小格式化函数合集

    比如碰到一个很大的文件有49957289167B,大家一看这么一长串的数字后面单位是字节B,还是不知道这个文件的大小是一个什么概念,我们把它转换成GB为单位,就是46.53GB.用下面这些函数就可以完 ...

  5. MySQL主从同步延迟

    早上接到open-falcon报警,一台mysql从库同步延迟2w多秒,mysql版本比较老,用的5.1.37. 连接从库查找原因: show processlist一下,查看哪些线程在跑. 看到Ti ...

  6. ToString和Convert.ToString处理null值

    http://www.cnblogs.com/qinge/p/5687806.html文章来源 1.Convert.ToString能处理字符串为null的情况. 测试代码如下: 1 2 3 4 5 ...

  7. ecshop 导出exl表格

    // 导出订单 if(isset($_POST['export'])){ // 统计金额 $sl = "SELECT SUM(goods_amount) as total from" ...

  8. live555库中的testH264VideoStreamer实例

    1.h264文件的推送 testH264VideoStreamer.cpp文件的开头就定义了 char const* inputFileName = "test.264"; 后面接 ...

  9. Symbiont

    http://www.weiyangx.com/209230.html Symbiont,Credit Suisse与R3携手革新贷款数据验证环节Symbiont, Credit Suisse and ...

  10. thinkphp一句话疑难解决笔记 2

    php中的_ _call()方法? 它是php5后为对象 类 新增的一个自动方法. 它会监视类的其他方法的调用, 当调用类的不存在的方法时, 会自动调用类的__call方法. tp的 "命名 ...