LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
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 only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
这道题的本质在于找出一个数组中任意两个数(序号大的数减去序号小的数)的最大差值。我们发现数组某一段的最大差值信息是可以保存的,并作为下一段的初始数值。具体的递推式如下:
dp[i] = max(dp[i - ], prices[i] - minPrice);
程序也是比较简单:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == )
return ;
vector<int> dp(sz, );
int minPrice = prices[];
for(int i = ; i < sz; i++)
{
dp[i] = max(dp[i - ], prices[i] - minPrice);
if(minPrice > prices[i])
minPrice = prices[i];
}
return dp[sz - ];
}
};
另一个很有代表性的解法如下(参考自一博文):
按照股价差价构成一个新的数组,即prices[1]-prices[0], prices[2]-prices[1], prices[3]-prices[2], ..., prices[n-1]-prices[n-2],这样我们的问题就转变成求最大的连续子段和。具体如何高效求解最大连续子段和请参考自我之前写过的一篇博文。
Best Time to Buy and Sell Stock II
LeetCode没有提供题目,源自其他博文。
题目要求:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
这道题的解法更加简单,我们这需要先构建一个股价差数组,然后把该数组中所有大于0的数相加就能够得到结果。具体程序引自同一篇博文:
class Solution {
public:
int maxProfit(vector<int> &prices) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int len = prices.size();
if(len <= )return ;
int res = ;
for(int i = ; i < len-; i++)
if(prices[i+]-prices[i] > )
res += prices[i+] - prices[i];
return res;
}
};
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 algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
因为只能完成最多两次交易,而且第二次交易只能在第一次交易完成后进行,因此我们可以先按第一题方法,按顺序求得最大和数组,再逆序求得最大和数组,最后再跟据这两个数组求得最大收益。具体程序如下:
class Solution {
public:
int simpleMaxProfit(vector<int>& prices) {
int sz = prices.size();
if(sz == )
return ;
vector<int> dp(sz, );
int minPrice = prices[];
for(int i = ; i < sz; i++)
{
dp[i] = max(dp[i - ], prices[i] - minPrice);
if(minPrice > prices[i])
minPrice = prices[i];
}
vector<int> dp_rev(sz, );
int maxPrice = prices[sz - ];
for(int i = sz - ; i > -; i--)
{
dp_rev[i] = min(dp_rev[i + ], prices[i] - maxPrice);
if(maxPrice < prices[i])
maxPrice = prices[i];
}
vector<int> maxGain(sz, );
maxGain[] = ;
for(int i = ; i < sz; i++)
{
maxGain[i] = max(maxGain[i - ], dp[i] - dp_rev[i]);
}
return maxGain[sz - ];
}
int maxProfit(vector<int>& prices) {
return simpleMaxProfit(prices);
}
};
LeetCode之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV的更多相关文章
- Best Time to Buy and Sell Stock I II III IV
一.Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of ...
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
题记:在求最大最小值的类似题目中,递推思想的奇妙之处,在于递推过程也就是比较求值的过程,从而做到一次遍历得到结果. LeetCode 上面的这三道题最能展现递推思想的美丽之处了. 题1 Best Ti ...
- [Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I 你只能一个操作:维修preMin拍摄前最少发生值 代码例如以下: int maxProfit(vector<int> & ...
- Best Time to Buy and Sell Stock I II III
Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...
- 解题思路:best time to buy and sell stock i && ii && iii
这三道题都是同一个背景下的变形:给定一个数组,数组里的值表示当日的股票价格,问你如何通过爱情买卖来发家致富? best time to buy and sell stock i: 最多允许买卖一次 b ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
随机推荐
- windows 消除文件名中的快捷方式
1)运行regedit进入注册表.2)依次打开:HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer3)右侧框图,把 ...
- activiti源码编译
个小时,大家安装的时候一定要耐心. 最终编译之后的效果部分如下图所示: 因为我这里有些xml文件没有去除验证,所以有红色的警告,不过也不影响使用. 下面看一下下载之后文件的变化如下图所示: 我们导入主 ...
- Activiti 流程部署方式 activi 动态部署(高级源码篇)
Activiti的流程 部署方式有很多种方式,我们可以根据activit工作流引擎提供的ap方式进行部署. 当然了实际需求决定你要使用哪一种api操作,后面的总结详细介绍了使用场景. 下面看一下部署方 ...
- 剑指Offer——简述堆和栈的区别
剑指Offer--简述堆和栈的区别 堆(Heap) Java堆是被所有线程共享的一块内存区域,在虚拟机启动时创建: Java虚拟机规范描述:所有的对象实例及数组都要在堆上分配: Java堆可以处于物理 ...
- android文件混淆详解
-injars androidtest.jar[jar包所在地址] -outjars out[输出地址] -libraryjars 'D:\android-sdk-windows\plat ...
- (一〇七)iPad开发之modal的切换方式与展示方式
在iPad上modal有四种切换方式,分别是竖直进入(由下到上,默认方式).水平翻转.淡入淡出. 属性要设置在将要modal出来的控制器上: /* typedef NS_ENUM(NSInteger, ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Android进阶(十)Android 发邮件
最近在做的APP涉及到发邮件,总结如下: 在android里进行邮件客户端开发可以有两种方式: 在邮件客户端的设计中,可以采用两种方法. 一种是调用android系统自带的邮件服务 优点:这种方法比较 ...
- hadoop队列管理(指定queue跑程序)
hadoop 升级到cdh5后,队列管理被取消,而是统一用资源池分配. hadoop2.0版本,Hadoop采用了平级队列组织方式,,管理员可将用户分到若干个扁平队列中,在每个队列中,可指定一个或几个 ...
- 海量数据挖掘MMDS week4: 推荐系统Recommendation System
http://blog.csdn.net/pipisorry/article/details/49205589 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...