121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
2016-05-04 17:57
363 查看
121.
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.
122.
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).
123.
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.
188.
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 k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
buy[i]表示买i个最多剩多少钱。sell[i]表示卖i个最多有多少钱。
和上面一个算法思路一样。
309.
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) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
sell[i-2]表示cooldown[i-1]。
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.
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n < 1) return 0; int mini = prices[0], ans = 0, i; for(i = 1; i < n; i++) { if(prices[i]-mini > ans) ans = prices[i]-mini; if(prices[i] < mini) mini = prices[i]; } return ans; } };
122.
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).
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n < 1) return 0; int mini = prices[0], maxi = prices[0], ans = 0, i; for(i = 1; i < n; i++) { if(prices[i] > maxi) maxi = prices[i]; else if(prices[i] < maxi) { ans += maxi - mini; maxi = mini = prices[i]; } } ans += maxi - mini; return ans; } };
123.
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.
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n < 1) return 0; vector<int> forward(n, 0), backward(n, 0); int mini, maxi, ans, i; forward[0] = 0; mini = prices[0]; for(i = 1; i < n; i++) { forward[i] = max(forward[i-1], prices[i] - mini); if(prices[i] < mini) mini = prices[i]; } backward[n-1] = 0; maxi = prices[n-1]; for(i = n-2; i >= 0; i--) { backward[i] = max(backward[i+1], maxi - prices[i]); if(prices[i] > maxi) maxi = prices[i]; } ans = 0; for(i = 0; i < n; i++) { ans = max(ans, forward[i] + backward[i]); } return ans; } };
188.
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 k 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 maxProfit(int k, vector<int>& prices) { int n = prices.size(), i, j; if(n < 1) return 0; if(k >= (n>>1)) { int ans = 0; for(i = 0; i < n-1; i++) { if(prices[i+1]-prices[i] > 0) ans += prices[i+1]-prices[i]; } return ans; } vector<int> buy(k+1, INT_MIN), sell(k+1, 0); for(i = 0; i < n; i++) { for(j = 1; j <= k; j++) { buy[j] = max(buy[j], sell[j-1] - prices[i]); sell[j] = max(sell[j], buy[j] + prices[i]); } } return sell[k]; } };
buy[i]表示买i个最多剩多少钱。sell[i]表示卖i个最多有多少钱。
buy[j] = max(buy[j], sell[j-1] - prices[i]); //看买prices[i]是否有原来划算
class Solution { public: int maxProfit(int k, vector<int>& prices) { int n = prices.size(), i, j; if(n < 1) return 0; if(k >= (n>>1)) { int ans = 0; for(i = 0; i < n-1; i++) { if(prices[i+1]-prices[i] > 0) ans += prices[i+1]-prices[i]; } return ans; } vector<vector<int>> dp(n, vector<int>(k+1, 0)); //dp[i][j]表示到第i天卖j个最多赚多少钱 for(i = 1; i <= k; i++) { int buy = -prices[0]; for(j = 0; j < n; j++) { dp[j][i] = max(j > 0 ? dp[j-1][i] : 0, buy + prices[j]); buy = max(buy, dp[j][i-1] - prices[j]); } } return dp[n-1][k]; } };
和上面一个算法思路一样。
309.
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) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:
prices = [1, 2, 3, 0, 2] maxProfit = 3 transactions = [buy, sell, cooldown, buy, sell]
class Solution { public: int maxProfit(vector<int>& prices) { int n = prices.size(); if(n <= 1) return 0; vector<int> sell(n+1, 0); int buy = -prices[0], i; for(i = 2; i <= n; i++) { sell[i] = max(sell[i-1], buy + prices[i-1]); buy = max(buy, sell[i-2] - prices[i-1]); } return sell ; } };
sell[i-2]表示cooldown[i-1]。
相关文章推荐
- ajax 设置Access-Control-Allow-Origin实现跨域访问
- Android 混淆配置及Bug处理
- Kryo-net •LibGDX
- Objective-C 的底层实现(Runtime)(转载)
- grep用法详解:grep与正则表达式
- Hadoop 2.2.0 集群搭建
- Linux下Shell中特殊变量$!,$?,$@,$#,$*的变量说明
- Hadoop 2.2.0 集群搭建
- Android zxing二维码扫描适应各种分辨率
- Spring Boot 部署与服务配置
- sql语句的预处理mysqli pdo绑定参数
- Spring Boot 部署与服务配置
- HTML5中Access-Control-Allow-Origin解决跨域问题
- 防止SQL注入和XSS攻击Filter
- Android 学习(1)——AndroidStudio安装以及Helloworld
- Java使用udp传输方式进行网络通信
- mybatis进行insert操作,得到自增的id
- css渲染-2
- linux 下配置tomcat
- JS基础笔记