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 ...
随机推荐
- 事务控制语言DTL
一.什么是事务? · 数据库中的事务,是指可以将“多条相关语句执行”看做是“一条语句执行”的一种内部机制.即事务是一种可以保证“多条语句一次性执行完成”或者一条语句都不执行的机制. 三.事务的特点 原 ...
- Linux 文本编辑常用快捷键
一.编辑模式 vim有三种编辑模式 1. i 进入文本编辑模式 2. esc 进入命令编辑模式 命令编辑状态下 dd删除整行 3. :进入底行模式 底行模式状态 输入q 退出 w保存 wq 保存并 ...
- Redis的安装、服务配置
在网上找了很多资料,有些可以正常安装,有些安装会出毛病,仔细想了想,还是自己整理一份吧,仅仅为自己下次再用的时候,能够快速的定位到可以正常用的文章! 我使用的是VMware Workstation P ...
- Codeforces Round #505 D. Recovering BST(区间DP)
首先膜一发网上的题解.大佬们tql. 给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1? 这个题场上是想暴力试试的.结 ...
- Latex:插入伪代码
*本文属于转载. *转载链接:https://blog.csdn.net/lwb102063/article/details/53046265 目录 clrscode algorithm algori ...
- dedecms 建站相关问题
1.栏目新建文章提示:模板文件不存在,无法解析文档! 解决方法:把模板文件使用".html"的格式 /include/arc.archives.class.php 556行 if ...
- Nginx从入门到放弃-第2章 基础篇
2-1 什么是Nginx 2-2 常见的中间件服务 2-3 Nginx的特性_实现优点1 2-4 Nginx特性_实现优点2 2-5 Nginx特性_实现优点3 2-6 Nginx特性_实现优点4 2 ...
- [UnicodeEncodeError]:Django中解决URL中文解释乱码问题
Django中在使用HttpResponseRedirect的时候,跳转URL中如果存在中文,会报错:会报UnicodeEncodeError错误. 解决办法: 使用urlquote对URL进行编码 ...
- 【Luogu】P3232游走(高斯消元解概率)
题目链接 参见远航之曲dalao的题解,我再写一遍的话就没啥意思了. #include<cstdio> #include<cstring> #include<algori ...
- ACM程序设计选修课——1043: Radical loves integer sequences(YY)
1043: Radical loves integer sequences Time Limit: 1 Sec Memory Limit: 128 MB Submit: 36 Solved: 4 ...