【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 ...
随机推荐
- Dapper-继续
好久没有来博客园了,最近刚好有点时间晚上,继续完善之前的orm orm自己用的比较多的还是EF,linq写着真的是很方便,但是EF最让人头疼的地方还是每个表都需要建立mapping. 这个是相当的烦恼 ...
- JDBC之代码优化
上一次我们是先实现了JDBC对数据库的增删查改操作,然后在增加新信息过程中发现了新的问题,即当某一操作失败,为了维护数据库的一致性,我们需要回滚事务.在其中我们了解了事务的工作原理及相关代码的使用. ...
- 学会WCF之试错法——数据传输
数据传输 服务契约 [ServiceContract] public interface IService { [OperationContract] string GetData(int value ...
- JavaScript的兼容
兼容总结 如果两个都是属性,用逻辑 || 做兼容 如果有一个是方法,用三元做兼容 如果多个属性或方法,封装函数做兼容 获取class属性值的兼容 function getClass (obj){ if ...
- NuGet的使用、部署、搭建私有服务
NuGet的使用.部署.搭建私有服务 前言 什么是NuGet? 为什么要使用NuGet NuGet的优点 使用 Get-Help NuGet Install-Package Get-Package U ...
- 深入理解Java内置锁和显式锁
synchronized and Reentrantlock 多线程编程中,当代码需要同步时我们会用到锁.Java为我们提供了内置锁(synchronized)和显式锁(ReentrantLock)两 ...
- web-iPhone X
题目: 解题思路: 第一次看到html里只有字其他啥也没有的题,一脸懵逼,学长提示抓包改包,于是开始我的苦逼解题. 0x01 抓包 0x02 改包 由于题目说只有iphoneX才能接受这个websit ...
- Vue-cli创建项目从单页面到多页面2-history模式
之前讲过怎样将vue-cli创建的项目改造成多页面(vue-cli创建项目从单页面到多页面),今天说一下怎样在多页面的前提下使用history模式. 如何使用history模式 因为vue默认的has ...
- 02-线性结构3 Reversing Linked List
题目 Sample Input: 00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 ...
- VC++下编译 程序“减肥”
在vc6 和 vs 2008下 编译 以下代码,不更改任何编译设置(vc6 40k , s2008 7k). 一.vc6下,Release 模式 编译处理. 1.去掉不必要的 链接库 工程(Pro ...