53. 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
.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
f[i] = max{f[i-1]+nums[i],nums[i]},1<=i<n
return max{f[i]} 0<=i<n
*/
class Solution {
public:
int maxSubArray(vector<int> &nums){
/**dp
f[i] = max{f[i-1]+nums[i],nums[i]},1<=i<n
return max{f[i]} 0<=i<n
*/ int length = nums.size();
vector<int> f(length);
f[] = nums[];
for(int i = ;i<length;i++){
f[i] = max(f[i-]+nums[i],nums[i]);
} vector<int>::iterator iter;
iter = max_element(f.begin(),f.end());
return *iter;
}
};
53. Maximum Subarray的更多相关文章
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LN : leetcode 53 Maximum Subarray
lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
随机推荐
- “LC.exe”已退出,代码为 -1
造成这个问题的原因一般是引入了第三方插件,自己遇到的问题是引入了devexpress...... 1.找到Properties文件夹licenses.licx文件,然后右键选择删除就可以了,调试运行正 ...
- Nginx reopen reload作用及工作过程
http://www.iigrowing.cn/nginx-reopen-reload-zuo-yong-ji-gong-zuo-guo-cheng.html Nginx reopen reload作 ...
- ITPUB网站的知识索引汇总
1. ITPUB知识索引树 http://www.itpub.net/tree/ http://www.itpub.net/pubtree/index.htm 2. ITPUB知识索引贴——全文索引 ...
- OC语言BLOCK和协议
OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似 ...
- iOS开发数据库篇—FMDB简单介绍
iOS开发数据库篇—FMDB简单介绍 一.简单说明 1.什么是FMDB FMDB是iOS平台的SQLite数据库框架 FMDB以OC的方式封装了SQLite的C语言API 2.FMDB的优点 使用起来 ...
- apache下自定义404错误页面
404页面的目的是:告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其他页面而不是关闭窗口离开. 很多开源系统包括CMS系统.Blog系统等不提供404页面或提供的404页面并未达到SE ...
- 全真模拟 (1) day1
第一题: 题目大意: 给出N个数的m对关系(a,b)表示a大于b. 每个数至少为100,求这些书最小可能的和. 解题过程: 1.看到这题就想到之前USACO的一道题,那题是N头牛排序,然后给出m对关系 ...
- java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
今天在使用动态代理时,遇到了如下问题,报错 java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice 下面是完整的报错信息: 一月 , :: ...
- MySql指令集
http://blog.csdn.net/cl05300629/article/details/9464007
- ubuntu下firefox浏览器flash player插件的安装
自从装了双系统后,ubuntu下的音乐软件只能选择网页播放器了,这无疑是需要播放插件的,这个插件就是falsh player. 当初使用usb启动盘安装的,在安装的过程中还会报找不到CD-rom的错, ...