题目:

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.

代码:

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int largest_sum = INT_MIN;
int tmp_sum = ;
for ( size_t i = ; i<nums.size(); ++i ){
tmp_sum += nums[i];
largest_sum = std::max(largest_sum, tmp_sum);
tmp_sum = tmp_sum> ? tmp_sum : ;
}
return largest_sum;
}
};

tips:

经典DP问题。

有个细节遗漏了导致开始几次没有AC。

DP公式“global_max = max ( global_max, local )”

这里的local应该是必须包含当前元素的局部最优值,因为每轮largest_sum在跟tmp_sum比较之前,tmp_sum都必须先加上当前元素。

比较过之后,如果发现tmp_sum是小于零的,肯定要把之前的都舍弃了(即之前的一个都不要了),这时再更新tmp_sum为0。

完毕。

=========================================

第二次过这道题,还是犯了第一次思维误区,但是很快纠正了,先获得一定包含Local的最优值,再与全局最优值比较。

class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ret = INT_MIN;
int local = INT_MIN;
for ( int i=; i<nums.size(); ++i )
{
local = local>= ? local+nums[i] : nums[i];
ret = max(local,ret);
}
return max(local,ret);
}
};

【Maximum Subarray 】cpp的更多相关文章

  1. leetcode 【 Maximum Subarray 】python 实现

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

  2. 【最大团】【HDU1530】【Maximum Clique】

    先上最大团定义: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题,在国际上已有广泛的研究,而国内对MCP问题的研究则还处于起步 ...

  3. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  4. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  5. 【Jump Game】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  6. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

随机推荐

  1. Android中intent相关,setFlag(xx);

    intent.setFlags(参数)://参数用法如下 :FLAG_ACTIVITY_CLEAR_TOP: 例如现在的栈情况为:A B C D .D此时通过intent跳转到B,如果这个intent ...

  2. 页面中插入视频兼容ie8以上的浏览器

    有时候页面中需要插入视频,如果不考虑ie8的话:就是直接用h5标签<video></video>就可以了: 但是有的时候需求是需要考虑ie8,这样我们就可以用插件,这种vide ...

  3. java Vamei快速教程02 方法和数据成员

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在Java基础01 从HelloWorld到面向对象,我们初步了解了对象(obje ...

  4. linux 命令——47 iostat (转)

    Linux系统中的 iostat 是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会 汇报出CPU使用情况 ...

  5. 论overflow滚动的重要性

    原理 设置一个块级作用域溢出的效果,只需要在外部块的位置上设置overflow:scroll和height:xx即可. 此时,块级作用域的内容位移超出外部块的位移就会出现滚动条,当内部块滚动时,我们能 ...

  6. Liunx开发(Extjs4.1+desktop+SSH2超强视频教程实践)(2)

    然后装eclipse: 为啥默认是搜狗导航: java还没装呢: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...

  7. <已解决>使用selector设置Button按下松开的样式以及 <item> tag requires a 'drawable' attribute or child tag defining a drawable 报错

    <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="ht ...

  8. [转载]AngularJS入门教程03:迭代器

    我们在上一步做了很多基础性的训练,所以现在我们可以来做一些简单的事情喽.我们要加入全文检索功能(没错,这个真的非常简单!).同时,我们也会写一个端到端测试,因为一个好的端到端测试可以帮上很大忙.它监视 ...

  9. 设置RichTextBox控件的文本的对齐方式

    实现效果: 知识运用: RichTextBox控件的SelectionAlignment属性 //获取或设置在当前选择或插入点的对齐方式 public HorizontalAlignment Sele ...

  10. 告诉你今年是哪个生肖年的java程序

    package com.swift; import java.util.Scanner; public class ChineseYear { public static void main(Stri ...