【Maximum Subarray 】cpp
题目:
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的更多相关文章
- leetcode 【 Maximum Subarray 】python 实现
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【最大团】【HDU1530】【Maximum Clique】
先上最大团定义: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题,在国际上已有广泛的研究,而国内对MCP问题的研究则还处于起步 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Jump Game】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
随机推荐
- Git命令--保存用户名和密码
使用git各项操作时,总是会出现输入密码的弹窗,且需要多次输入,很是繁琐,通过git命令可以记住密码,避免多次操作. 一.创建保存密码的文件 1.在home文件夹,一般是 C:\Documents a ...
- 微信公众平台网页开发实战--3.利用JSSDK在网页中获取地理位置(HTML5+jQuery)
复制一份JSSDK环境,创建一份index.html文件,结构如图7.1所示. 图7.1 7.1节文件结构 在location.js中,封装“getLocation”接口,如下: 01 wxJSSD ...
- 微软高性能缓存AppFabric (一) 安装
博客原文链接:http://www.cnblogs.com/Qbit/p/6088703.html AppFabric 缓存功能的前身是VeloCity ,它是基于windows平台的一个高速内存缓存 ...
- Codeforces Round #324 (Div. 2) A B C D E
A,水题不多说. #include<bits/stdc++.h> using namespace std; //#define LOCAL int main() { #ifdef LOCA ...
- hdu-1233 还是畅通工程---MST模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 题目大意: 求MST最小生成树 解题思路: Prim算法直接套即可 #include<b ...
- 【转】iOS 文件下载及断点续传
ios的下载我们可以使用的方法有:NSData.NSURLConnection.NSURLSession还有第三方框架AFNetworking和ASI 利用NSData方法和NSURLConnecti ...
- 面向服务架构SOA
面向服务的体系结构(Service-Oriented Architecture,SOA)是一个组件模型.它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来:接口是采用中 ...
- 求最大公约数和最小公倍数_python
"""写两个函数,分别求两个整数的最大公约数和最小公倍数,调用这两个函数,并输出结果.两个整数由键盘输入.""" ''' 设两个整数u和v, ...
- 前端小记3——iOS与Android问题
1.消除transition闪屏 (1)-webkit-transform-style:preserve-3d; /*设置内嵌的元素在 3D 空间如何呈现:保留 3D*/ (2)-webkit-ba ...
- 2.3.3 zerosum 和为零
#include<bits/stdc++.h> using namespace std; ],a; ]={' ','+','-'}; void out() { ;i<a;i++) c ...