Best Time to Buy and Sell Stock
来源:互联网 发布:html css javascript 编辑:程序博客网 时间:2024/06/05 00:40
本题有一共有四中问题
1.
Say you have an array for which the ith element is the price of a given stock on dayi.
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.
从前向后,同时记录最小交易价格,到第i天的最大利润
class Solution {public: int maxProfit(vector<int>& prices) { int pricelength=prices.size(); if(pricelength==0) return 0; //vector<int> profit(pricelength-1); int minprice=prices[0]; int maxprofit=0; for(int i=1; i<pricelength; i++) { if(prices[i]<minprice) minprice=prices[i]; else { if((prices[i]-minprice)>maxprofit) { maxprofit=(prices[i]-minprice); } } } return maxprofit; }};
2.
Say you have an array for which the ith element is the price of a given stock on dayi.
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).
解析:每天的价格看作一个曲线,每个上升段的增加值累加,即每个极小值到极大值的增量累加,实现时相邻两天只要增加就对ans进行累加代码:
class Solution {public: int maxProfit(vector<int>& prices) { int n=prices.size(); int ret=0; for(int i=1; i<n; i++) { ret+=max(prices[i]-prices[i-1],0); } return ret; }};
3.
Say you have an array for which the ith element is the price of a given stock on dayi.
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).
在这里定义global[i][j]为前i天交易j次的最大收益,local[i][j]表示前i天交易j次的最大收益,且第i天进行了第j次交易
diff=price[i]-price[i-1],
状态转移方程为local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff));
global[i][j]=max(local[i][j],global[i-1][j]);
对于本题j最大为2
代码:
class Solution {public: int maxProfit(vector<int>& prices) { if (prices.empty()||prices.size()<2) return 0; int day=prices.size(); vector<vector<int>>global(prices.size()+1,vector<int>(4,0)); vector<vector<int>>local(prices.size()+1,vector<int>(4,0)); for (int i=1; i<=day; i++) { int diff=prices[i-1]-prices[i-2]; if (i==1) diff=0; for (int j=1; j<3; j++) { local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff)); global[i][j]=max(local[i][j],global[i-1][j]); } } return global[day][2]; }};
4.
Say you have an array for which the ith element is the price of a given stock on dayi.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
a. 由于3中时间复杂度为O(n*k),在本题中如果k>n会tl ,这是就是可以交易任意次,按题2求解
b. 3中空间复杂度为O(n*k*2),在本题中可能会ml,所以本题中可以使用滚动数组
代码:
class Solution {public: int maxProfit(int k, vector<int>& prices) { if (prices.empty()||prices.size()<2) return 0; int day=prices.size(); if(k>day) return maxprofit(prices); vector<vector<int>>global(prices.size()+1,vector<int>(k+1,0)); vector<vector<int>>local(2,vector<int>(k+1,0)); for (int i=1; i<=day; i++) { int diff=prices[i-1]-prices[i-2]; if (i==1) diff=0; for (int j=1; j<k+1; j++) { local[i%2][j]=max(local[(i+1)%2][j]+diff,global[i-1][j-1]+max(0,diff)); global[i][j]=max(local[i%2][j],global[i-1][j]); } } return global[day][k]; } int maxprofit(vector<int>& prices) { int ans=0; for (int i=1; i<prices.size(); i++) { ans+=max(0,prices[i]-prices[i-1]); } return ans; } };
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time To Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock
- bootbox.js——弹框插件
- 日志级别的选择:Debug、Info、Warn、Error还是Fatal
- 弄死reactjs1
- 第八届蓝桥杯_分巧克力_二分
- 如何更换Android studio 风格
- Best Time to Buy and Sell Stock
- 腾讯云安装Apache+Php+Mysql教程
- Java中转UTC时间字符串(含有T Z)为local时间
- CASIA情感数据库
- getString()方法用法详解
- Android中解决在ListView历史复用中Edittext数据显示混乱
- oracle 存储过程 小例子
- CSS3 3D动画 transform
- 64位win7安装Oracle11gR2数据库安装详细过程