best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值
1、
Say you have an array for which the i th 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.
给一个数prices[],prices[i]代表股票在第i天的售价,求出只做一次交易(一次买入和卖出)能得到的最大收益。
只需要找出最大的差值即可,即 max(prices[j] – prices[i]) ,i < j。一次遍历即可,在遍历的时间用遍历low记录 prices[o....i] 中的最小值,就是当前为止的最低售价,时间复杂度为 O(n)。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty())
return ;
int res=,min=prices[];
for(int i=;i<prices.size();i++){
if(prices[i]<min) min=prices[i];
else if(prices[i]-min>res) res=prices[i]-min;
}
return res;
}
};
2、
Say you have an array for which the i th 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).
此题和上面一题的不同之处在于不限制交易次数。也是一次遍历即可,只要可以赚就做交易。
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int res =;
for(int i=;i<prices.size();i++){
if(prices[i]-prices[i-]>)
res+=prices[i]-prices[i-];
}
return res;
}
};
3、
Say you have an array for which the i th 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.
此题是限制在两次交易内,相对要难一些。容易想到的解决办法是,把prices[] 分成两部分prices[0...m] 和 prices[m...length] ,分别计算在这两部分内做交易的做大收益。由于要做n次划分,每次划分可以采用 第一题: I的解法, 总的时间复杂度为O(n^2).
public class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
for(int m = 0; m<prices.length; m++){
int tmp = maxProfitOnce(prices, 0, m) + maxProfitOnce(prices, m, prices.length-1);
if(tmp > ans) ans = tmp;
}
return ans;
}
public int maxProfitOnce(int[] prices,int start, int end){
if(start >= end) return 0;
int low = prices[start];
int ans = 0;
for(int i=start+1; i<=end; i++){
if(prices[i] < low) low = prices[start];
else if(prices[i] - low > ans) ans = prices[i] - low;
}
return ans;
}
}
但是由于效率过低,运行超时。可以利用动态规划的思想进行改进,保持计算的中间结果,减少重复的计算。
那就是第一步扫描,先计算出子序列[0,...,i]中的最大利润,用一个数组保存下来,那么时间是O(n)。计算方法也是利用第一个问题的计算方法。 第二步是逆向扫描,计算子序列[i,...,n-1]上的最大利润,这一步同时就能结合上一步的结果计算最终的最大利润了,这一步也是O(n)。 所以最后算法的复杂度就是O(n)的。
就是说,通过预处理,把上面的maxProfitOnce()函数的复杂度降到O(1)
class Solution {
public:
int maxProfit(vector<int> &prices) {
if(prices.empty()) return ;
int n=prices.size();
vector<int> opt(n,);
int res=,low=prices[];
for(int i=;i<n;i++){
if(prices[i]<low) low=prices[i];
else if(res <prices[i]-low) res=prices[i]-low;
opt[i]=res;
}
vector<int> optReverse(n,);
int high=prices[n-];
res=;
for(int i=n-;i>=;i--){
if(prices[i]>high) high=prices[i];
else if(high-prices[i]>res) res=high-prices[i];
optReverse[i]=res;
}
res=;
for(int i=;i<n;i++){
int tmp=opt[i]+optReverse[i];
res=tmp>res?tmp:res;
}
return res;
}
};
best-time-to-buy-and-sell-stock系列——先买入后卖出股票的最大值的更多相关文章
- 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 we ...
- LeetCode -- Best Time to Buy and Sell Stock系列
Question: Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pri ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【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] 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 ...
- 【LeetCode】Best Time to Buy and Sell Stock IV
Best Time to Buy and Sell Stock IV Say you have an array for which the ith element is the price of a ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- Maximum Subarray / Best Time To Buy And Sell Stock 与 prefixNum
这两个系列的题目其实是同一套题,可以互相转换. 首先我们定义一个数组: prefixSum (前序和数组) Given nums: [1, 2, -2, 3] prefixSum: [0, 1, 3, ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Python中__get__ ,__getattr__ ,__getattribute__用法与区别?
class C(object): a = 'abc' def __getattribute__(self, *args, **kwargs): print("__getattribute__ ...
- Leetcode 476.数字的补数
数字的补数 给定一个正整数,输出它的补数.补数是对该数的二进制表示取反. 注意: 给定的整数保证在32位带符号整数的范围内. 你可以假定二进制数不包含前导零位. 示例 1: 输入: 5 输出: 2 解 ...
- [svn学习篇]svn使用教程
http://www.cnblogs.com/longshiyVip/p/4905901.html http://blog.csdn.net/dily3825002/article/details/6 ...
- kb-09-线段树--区间合并比较繁
/* hdu-1540 题意:一个线段,长度为n,三种操作,Dx,挖掉某个点:R,恢复最近被挖掉的点:Qx查询该点所在的连续区间的长度: 树的节点维护三个变量,该节点左边界开始连续的个数ll,右边界开 ...
- 修改Tomcat HTTP端口号(8080→8088)Eclipse
1.如果不用Eclipse,修改Tomcat的端口号,可以参考这篇: https://jingyan.baidu.com/article/adc815139b12def722bf7377.html t ...
- [USACO12MAR]花盆Flowerpot (单调队列,二分答案)
题目链接 Solution 转化一下,就是个单调队列. 可以发现就是一段区间 \([L,R]\) 使得其高度的极差不小于 \(d\) ,同时满足 \(R-L\) 最小. 然后可以考虑二分然后再 \(O ...
- [转] Makefile 基础 (8) —— Makefile 隐含规则
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客:(最原始版本) http://blog.csdn.net/haoel/article/details/2886 我转自 ...
- CentOS 7.4升级Linux内核
CentOS 7.4升级Linux内核 [日期:2018-01-15] 来源:Linux社区 作者:straycats [字体:大 中 小] 由于最近intel出了Meltdown和Spectre两 ...
- 百万级日活 App 的屏幕录制功能是如何实现的
Android 从 4.0 开始就提供了手机录屏方法,但是需要 root 权限,比较麻烦不容易实现.但是从 5.0 开始,系统提供给了 App 录制屏幕的一系列方法,不需要 root 权限,只需要用户 ...
- 【POJ1144】Network(割点)(模板)
题意:给定一张无向图,求割点个数 思路:感谢CC大神http://ccenjoyyourlife.blog.163.com/的讲解 割点的定义就是某个联通块中删去此点连通性发生变化的的点 有两种割点: ...