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 ...
随机推荐
- storm如何部署拓扑
storm集群搭建 比较简单,参考官方文档即可http://storm.apache.org/releases/1.0.2/Setting-up-a-Storm-cluster.html 启动Nimb ...
- java多线程的编程实例
java中可有两种方式实现多线程: 一种是继承Thread类: 一种是实现Runnable接口: Thread类 是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的ru ...
- GDAL不支持创建PCIDSK的面状矢量格式
最近在使用GDAL创建PCIDSK格式的矢量数据,发现创建点和线的矢量数据都没问题,创建面状的只有属性表没有图形.在GDAL官网说明也写的是支持的,地址为:http://www.gdal.org/fr ...
- Citrix 桌面虚拟化解决方案与VMware桌面虚拟化解决方案对比
通过 XenDesktop 和 FlexCast为各种场景交付虚拟桌面 企业桌面面临的问题 为每个用户提供安全高效的桌面环境是几乎所有公司或组织的基本要求.如果用户无法使用他们的桌面或应用程序,公司就 ...
- Linux内核分配内存的方式
page = alloc_pages(GFP_KERNEL, get_order(1234));分配失败返回NULLGFP_KERNEL ---> 分配标志,当没有足够内存分配时,睡眠阻塞,直 ...
- ubuntu常用文件搜索命令
1.find find [搜索路径] [搜索关键字] 比如查找/test中文件名为t5.tmp的文件: 查找根目录下大于100M的文件 注意,这里的204800单位是块,1块=512字节 在根目录下查 ...
- debian 安装jdk
JDK下载http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6- ...
- React Native移植原生Android
(一)前言 之前已经写过了有关React Native移植原生Android项目的文章,不过因为RN版本更新的原因吧,跟着以前的文章可能会出现一些问题,对于初学者来讲还是会有很多疑难的困惑的,而且官方 ...
- Java学习从菜鸟变大鸟之一 hashCode()和equals()的本质区别和联系
在学习java,根据视频做实例的过程中,对equals和hashcode两个方法理解稍微深刻一点,主要是它们两个很容易混淆,容易出错,自己又通过网上的资料学习,和大家分享 equals()方法 equ ...
- 存储那些事儿(三):OpenStack的块存储Cinder与商业存储的融合
OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算软件,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStack是IaaS(基础设施即服务)软 ...