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 ...
随机推荐
- ROS探索总结(十九)——如何配置机器人的导航功能
1.概述 ROS的二维导航功能包,简单来说,就是根据输入的里程计等传感器的信息流和机器人的全局位置,通过导航算法,计算得出安全可靠的机器人速度控制指令.但是,如何在特定的机器人上实现导航功能包的功能, ...
- Spring基础配置
从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...
- Linux动态频率调节系统CPUFreq之三:governor
在上一篇文章中,介绍了cpufreq的core层,core提供了cpufreq系统的初始化,公共数据结构的建立以及对cpufreq中其它子部件提供注册功能.core的最核心功能是对policy的管理, ...
- 【学习笔记】启动Nginx、查看nginx进程、查看nginx服务主进程的方式、Nginx服务可接受的信号、nginx帮助命令、Nginx平滑重启、Nginx服务器的升级
1.启动nginx的方式: cd /usr/local/nginx ls ./nginx -c nginx.conf 2.查看nginx的进程方式: [root@localhost nginx] ...
- hive的strict模式;where,group by,having,order by同时使用的执行顺序
主要限制三种情况 (1) 有partition的表查询需要加上where子句,筛选部分数据实现分区裁剪,即不允许全表全分区扫描,防止数据过大 (2) order by 执行时只产生一个reduce,必 ...
- Ubuntu15.10 安装OpenCV3.1
wget https://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip/downloa ...
- UNIX网络编程——非阻塞accept
当有一个已完成的连接准备好被accept时,select将作为可读描述符返回该连接的监听套接字.因此,如果我们使用select在某个监听套接字上等待一个外来连接,那就没有必要把监听套接字设置为非阻塞, ...
- Android开发学习之路--UI之初体验
之前都是学习Activity,对于布局都没有做过学习,这里就简单学习下吧.下面看下Android Studio下有哪些控件: 这里分为Widgets,Text Fields,Containers,Da ...
- SQL join 语句 画图果然更容易理解
我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的.不过正如在她的帖子 ...
- UNIX环境高级编程——守护进程列表
amd:自动安装NFS(网络文件系统)守侯进程apmd:高级电源治理Arpwatch:记录日志并构建一个在LAN接口上看到的以太网地址和ip地址对数据库Autofs:自动安装治理进程automount ...