121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
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.
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n < )
return ;
int mini = prices[], ans = , i;
for(i = ; 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 < )
return ;
int mini = prices[], maxi = prices[], ans = , i;
for(i = ; 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 < )
return ;
vector<int> forward(n, ), backward(n, );
int mini, maxi, ans, i;
forward[] = ;
mini = prices[];
for(i = ; i < n; i++)
{
forward[i] = max(forward[i-], prices[i] - mini);
if(prices[i] < mini)
mini = prices[i];
}
backward[n-] = ;
maxi = prices[n-];
for(i = n-; i >= ; i--)
{
backward[i] = max(backward[i+], maxi - prices[i]);
if(prices[i] > maxi)
maxi = prices[i];
}
ans = ;
for(i = ; 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 < )
return ;
if(k >= (n>>))
{
int ans = ;
for(i = ; i < n-; i++)
{
if(prices[i+]-prices[i] > )
ans += prices[i+]-prices[i];
}
return ans;
}
vector<int> buy(k+, INT_MIN), sell(k+, );
for(i = ; i < n; i++)
{
for(j = ; j <= k; j++)
{
buy[j] = max(buy[j], sell[j-] - 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 < )
return ;
if(k >= (n>>))
{
int ans = ;
for(i = ; i < n-; i++)
{
if(prices[i+]-prices[i] > )
ans += prices[i+]-prices[i];
}
return ans;
}
vector<vector<int>> dp(n, vector<int>(k+, )); //dp[i][j]表示到第i天卖j个最多赚多少钱
for(i = ; i <= k; i++)
{
int buy = -prices[];
for(j = ; j < n; j++)
{
dp[j][i] = max(j > ? dp[j-][i] : , buy + prices[j]);
buy = max(buy, dp[j][i-] - prices[j]);
}
}
return dp[n-][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 <= )
return ;
vector<int> sell(n+, );
int buy = -prices[], i;
for(i = ; i <= n; i++)
{
sell[i] = max(sell[i-], buy + prices[i-]);
buy = max(buy, sell[i-] - prices[i-]);
}
return sell[n];
}
};
sell[i-2]表示cooldown[i-1]。
121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票的更多相关文章
- 领扣-121/122/123/188 最佳买卖时机 Best Time to Buy and Sell MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Leetocode7道买卖股票问题总结(121+122+123+188+309+901+714)
题目1----121. 买卖股票的最佳时机I: 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 给定一个数组, ...
- LeetCode No.121,122,123
No.121 MaxProfit 买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你 ...
- [LeetCode] 309. 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 ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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] 188. Best Time to Buy and Sell Stock IV 买卖股票的最佳时间 IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- QT 加载c语言编译的动态库
QLibrary lib("./libprint.so");//库的路径if(lib.load()){ typedef void(*AddFunction)(char *st ...
- ServiceStack.OrmLite 笔记4 删
删除 db.DeleteAll(); //各种姿势 db.Delete(p => p.Age == 27);// db.Delete(q => q.Where(p => p.Age ...
- Android——android必看 各个控件属性(网上看到的文字,觉得挺好的,珍藏了)
属性 值 说明 Android:orientation horizontal/vertical 设置布局水平还是垂直,默认是垂直 android:checked true/false 标记默认选中,如 ...
- 关于js运动的一些总结
js运动实现,有两种.一种是速度版,另一种是时间版. 速度版是通过对速度的加减乘除,得出元素的运动数据.时间版是通过对时间进行Tween公式运算,得出元素的运动数据. 速度版运动优点:容易在运动过程中 ...
- FLASH CC 2015 CANVAS (三) flash中写JS调用html中JS的函数,变量
注意 此贴 为个人边“开荒”边写,所以不保证就是最佳做法,也难免有错误! 正式教程会在后续开始更新 首先我们在HTML里的JS里面添加几行代码 我们在FLASH中新建一个元件,并拖入到舞台,在属性面板 ...
- Oracle VM VirtualBox 虚拟机与主机共享文件
安装增强功能(参考文档) VirtualBox自带了一个增强工具Sun VirtualBox Guest Additions,这是实现虚拟机与真实主机共享的关键.启动虚拟机后,点击控制菜单“设备”→“ ...
- POJ-2175 Evacuation Plan 最小费用流、负环判定
题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...
- iOS - OC 语言新特性
前言 相对于 Java,OC 语言是一门古老的语言了,而它又是一门不断发展完善的语言.一些新的编译特性,为 OC 语言带来了许多新的活力.在 Xcode7 中,iOS9 的 SDK 已经全面兼容了 O ...
- CodeBlocks使用技巧
快键键 注释:选中后Shfit + C 取消注释:选中后Shfit + X 查找替换:Ctrl + R Build(Ctrl + F9) Run (Ctrl + F10) Build + Run (F ...
- Differences between volume, partition and drive
A drive is a physical block disk. For example: /dev/sda. A partition A drive can be divided into som ...