【leetcode】123. Best Time to Buy and Sell Stock III
@requires_authorization
@author johnsondu
@create_time 2015.7.22 19:04
@url [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)
/************************
* @description: dynamic programming.
* 从前后分别求出当前所能够得到的最大值,最后相加而得
* 详细操作是,设置一个最小值,然后不断更新最小值。然后不断更新最大值。
前后反向累加求得最大值
* @time_complexity: O(n)
* @space_complexity: O(n)
************************/
class Solution {
public:
int maxProfit(vector<int>& prices) {
const int len = prices.size();
if(len < 2) return 0;
int maxFromHead[len];
maxFromHead[0] = 0;
int minprice = prices[0], maxprofit = 0;
for(int i = 1; i < len; i ++) {
minprice = min(prices[i-1], minprice);
if(maxprofit < prices[i] - minprice)
maxprofit = prices[i] - minprice;
maxFromHead[i] = maxprofit;
}
int maxprice = prices[len-1];
int res = maxFromHead[len-1];
maxprofit = 0;
for(int i = len-2; i >= 0; i --) {
maxprice = max(maxprice, prices[i+1]);
if(maxprofit < maxprice - prices[i])
maxprofit = maxprice - prices[i];
if(res < maxFromHead[i] + maxprofit)
res = maxFromHead[i] + maxprofit;
}
return res;
}
};
@requires_authorization
@author johnsondu
@create_time 2015.7.22 19:04
@url [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)
/************************
* @description: dynamic programming.
* 超时做法:从1開始。分成前后两段,最后求得前后两段的最大值
* @time_complexity: O(n^2)
* @space_complexity: O(n)
************************/
class Solution {
public:
int maxProfit(vector<int>& prices) {
int p_size = prices.size();
if(p_size < 2) return 0;
if(p_size < 3) {
return prices[1] - prices[0] > 0 ?
prices[1] - prices[0]: 0;
}
int ans = 0;
for(int i = 1; i < p_size; i ++) {
vector<int> dp1, dp2;
for(int j = 1; j <= i; j ++) {
dp1.push_back(prices[i] - prices[i-1]);
}
for(int j = i + 1; j < p_size; j ++) {
dp2.push_back(prices[j] - prices[j-1]);
}
int dp1_size = dp1.size();
int ans1 = 0;
int cur = 0;
for(int j = 0; j < dp1_size; j ++) {
cur = cur + dp1[j];
if(cur < 0) {
cur = 0;
continue;
}
if(cur > ans1) ans1 = cur;
}
int dp2_size = dp2.size();
int ans2 = 0;
cur = 0;
for(int j = 0; j < dp2_size; j ++) {
cur = cur + dp2[j];
if(cur < 0) {
cur = 0;
continue;
}
if(cur > ans2) ans2 = cur;
}
ans = max(ans, ans1 + ans2);
}
return ans;
}
};
【leetcode】123. Best Time to Buy and Sell Stock III的更多相关文章
- 【LeetCode】123. Best Time to Buy and Sell Stock III 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- LeetCode OJ 123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
题目如下: Your are given an array of integers prices, for which the i-th element is the price of a given ...
- 【LeetCode】121. Best Time to Buy and Sell Stock 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 C++ 解法 日期 ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】714. Best Time to Buy and Sell Stock with Transaction Fee 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
随机推荐
- MVC架构下,使用NPOI读取.DOCX文档中表格的内容
1.使用NPOI,可以在没有安装office的设备上读wiod.office.2.本文只能读取.docx后缀的文档.3.MVC架构中,上传文件只能使用form表单提交,转到控制器后要依次实现文件上传. ...
- 面向亿万级用户的QQ一般做什么?——兴趣部落的Web同构直出分享
作者:李强,腾讯web开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/348.html 一.什么是同构 ...
- MyEclipse和Eclipse非常方便的快捷键
1. ctrl+shift+r:打开资源这可能是所有快捷键组合中最省时间的了.这组快捷键可以让你打开你的工作区中任何一个文件,而你只需要按下文件名或mask名中的前几个字母,比如applic*.xml ...
- Windows Forms DataGridView中合并单元格
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...
- python基础教程——函数
定义函数 //abstest.py def my_abs(x): if x >= 0: return x else: return -x 在该文件的当前目录下启动python解释器,用 from ...
- lua 函数调用1 -- 闭包详解和C调用
这里, 简单的记录一下lua中闭包的知识和C闭包调用 前提知识: 在lua api小记2中已经分析了lua中值的结构, 是一个 TValue{value, tt}组合, 如果有疑问, 可以去看一下 一 ...
- Nosql简介 Redis,Memchche,MongoDb的区别
本篇文章主要介绍Nosql的一些东西,以及Nosql中比较火的三个数据库Redis.Memchache.MongoDb和他们之间的区别.以下是本文章的阅读目录 一.Nosql介绍 1.Nosql简介 ...
- MySQL错误:2003-Can't connect to MySQL server on 'localhost'(10061 "unknown error")
今天数据库出了一点错误之后决定重装一下,结果卡在了一个问题上,连装了5遍,加上网上各种配置教程都没能结局,错误如下图所示: 最后忽然想到会不会是因为每一次卸载的时候没有彻底卸载干净,然后就彻彻底底卸载 ...
- Python开发目录
Python开发目录 Python之三目运算符 Python之文件的基本操作
- Spring-MVC理解之一:应用上下文webApplicationContext
一.先说ServletContext javaee标准规定了,servlet容器需要在应用项目启动时,给应用项目初始化一个ServletContext作为公共环境容器存放公共信息.ServletCon ...