LeetCode OJ--Best Time to Buy and Sell Stock III
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
这三道题,很好的进阶。1题简单处理,2题使用贪心,3题使用动态规划。
话说这叫一维动态规划,嗯。又复习了《算法导论》中和贪心以及动态规划有关的知识,记录如下:
动态规划的标志:最优子结构、子问题重叠。
1.找最优子结构
2.定义递归公示(列一个式子出来,并定义好这个式子到底是什么意思)。
3.写自底向上或递归备忘录法。
比如本问题:f(i,j) = max{f(i,k)+f(k,j)} 其中:f(i,j)表示从i到j的所有数,进行一次交易能获得的最多的钱数。最终要求的是f(0,n-1),k从1到n-2.
再深化一下:
fp(i,j)表示从i 到 j 进行最多进行 p 次交易可以获得的钱数,则:
fp(i,j) = max {fp-1(i,j), fp-p'(i,k)+fp'(k,j)} 其中:p'从1到p-1,k从i+1到j-1.
这大概叫二维动态规划(或许)。
贪心算法是从局部最优,能够做到全局最优。可分成一步步的,当前的选择受之前做过的选择的影响。而动态规划,是受后面要做的选择的影响。
于是有了下面代码,复杂度O(n2)然后超时了。
class Solution {
public:
//start 第一个元素,end 最后一个元素的下标
int onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
}
return ans;
}
int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ;
int ans = onceBuyAndSell(prices,,prices.size()-);
if(prices.size()< || ans == )
return ans;
int temp = ;
for(int itor = ;itor<prices.size()-;itor++)
{
temp = onceBuyAndSell(prices,,itor) + onceBuyAndSell(prices,itor+,prices.size()-);
if(temp> ans)
ans = temp;
}
return ans;
}
};
使用了动态规划中打表的方法,降低了复杂度O(n).代码如下:
class Solution {
public:
vector<int> ansN;
//ansN[i]表示,从i到最后的所有元素,只进行一次交易的话,可以得到的最多值。
void onceBuyAndSell(vector<int> &prices,int start,int end)
{
int ans = ;
int max = prices[end];
ansN[end] = ;
for(int i = end - ;i>=start;i--)
{
if(max - prices[i]>ans)
ans = max - prices[i];
if(prices[i]>max)
max = prices[i];
ansN[i] = ans;
}
}
int maxProfit(vector<int> &prices) {
if(prices.empty())
return NULL;
if(prices.size()==)
return ;
ansN.resize(prices.size());
//先来一遍从后面计算的。
onceBuyAndSell(prices,,prices.size()-);
if(prices.size()< )
return ansN[];
int min = prices[];
int ans2 = ;
int ans = ansN[];
for(int itor = ;itor<prices.size()-;itor++)
{
if(prices[itor] - min>ans2)
ans2 = prices[itor] - min;
if(prices[itor]<min)
min = prices[itor];
if(ans2 + ansN[itor+]> ans)
ans = ans2 + ansN[itor+];
}
return ans;
}
};
LeetCode OJ--Best Time to Buy and Sell Stock III的更多相关文章
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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】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]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 ...
- LN : leetcode 123 Best Time to Buy and Sell Stock III
lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...
- [LeetCOde][Java] 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 a ...
- [LeetCode OJ] Best Time to Buy and Sell Stock I
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LeetCode OJ - Best Time to Buy and Sell Stock
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/xiezhihua120/article/details/32939749 Say you have ...
- Java for LeetCode 123 Best Time to Buy and Sell Stock III【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 123. Best Time to Buy and Sell Stock III ----- java
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode 【 Best Time to Buy and Sell Stock III 】python 实现
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
随机推荐
- thinkcmf5增加微信管理app笔记
simplewind/extend/目录下增加 EasyWeChat Monolog //是PHP的一个日志类库 https://segmentfault.com/a/1190000002775 ...
- LeetCode(128) Longest Consecutive Sequence
题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence ...
- Mysql主键一致时,可以进行在元数据上的操作
insert into daliy_hit_counter(day,slot,cnt) values(12,12,1) on duplicate key update cnt = cnt +1 dal ...
- UML结构与解析——BUAA OO第四单元作业总结
UML与解析架构 UML是什么 统一建模语言(英语:Unified Modeling Language,缩写 UML)是非专利的第三代建模和规约语言.UML是一种开放的方法,用于说明.可视化.构建和编 ...
- BZOJ 5215: [Lydsy2017省队十连测]商店购物
裸题 注意+特判 #include<cstdio> using namespace std; const int mod=1e9+7; int F[1000005],mi[10000005 ...
- mysql update连表
UPDATE price_air_item t1 LEFT JOIN order_item t2 ON t1.ORDER_ITEM_ID = t2.ORDER_ITEM_ID SET t1.BUYER ...
- ogre3D学习基础11 -- 日志文件的使用与异常处理
用文件来记录 Ogre 系统初始化.运行.结束以及调试信息.使用日志便于我们调试程序.Ogre 日志系统由两个类组成:Log 类与 LogManager. 1.Log类 Log 类的一个对象对应于一个 ...
- day03_01 Python历史、32bit和64bit系统的区别
先看一下讲师的笔记,有python介绍 在python2.6版本之后,想清理一些东西,追求简单明了,就直接升级到了python3.0 但是python3.0导致很多企业都不更新,因为有很多企业的网站代 ...
- PHP协程是通过生成器实现的,这里测试了PHP生成器的一些特性
学习PHP的生成器,测试了一些特性.代码如下: function gen() { $name = (yield 'hello'); $nickname = (yield 'world'); yield ...
- 区别Transform、Transition、Animation
另一篇参考文章:http://www.7755.me/Article/CSS3/39/ 近来上班之外就是研究研究CSS动画,下面是第一阶段总结.话说为加强记忆,实则想抛砖引玉! 标题直译一下就是: ...