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 -- 买卖股票的更多相关文章

  1. 领扣-121/122/123/188 最佳买卖时机 Best Time to Buy and Sell MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  2. Leetocode7道买卖股票问题总结(121+122+123+188+309+901+714)

    题目1----121. 买卖股票的最佳时机I: 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 给定一个数组, ...

  3. LeetCode No.121,122,123

    No.121 MaxProfit 买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你 ...

  4. [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 ...

  5. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Pre-Query trigger in Oracle D2k / Oracle Forms

    Pre-Query trigger in Oracle D2k / Oracle Forms DescriptionFires during Execute Query or Count Query ...

  2. [HRBUSTOJ1476]Pairs(FFT)

    题目链接:http://acm-software.hrbust.edu.cn/problem.php?id=1476 题意:给n个数,m次询问,每次询问一个k.问n个数里两数之和严格小于k的数对. 根 ...

  3. Beaglebone Black–GPIO 高低电平控制 LED 灯

    上一篇,运用 Linux 的 sysfs,控制本机上的 LED 灯,usr0 至 usr3,这次用 GPIO 控制外部的电路,点亮 LED 灯. 这次的全部材料: BBB 一台 购买 BBB 自带的 ...

  4. EditPlus添加到右键菜单

    1.Alt+R 键打开“运行” 2.“运行”中输入:regedit 打开注册表    (1.在 "我的电脑HKEY_CLASSES_ROOT*" 下新建项 shell: (2.在 ...

  5. 测算Redis处理实际生产请求的QPS/TPS

    测算Redis处理实际生产请求的QPS/TPS Benchmark工具 redis发布版本中自带了redis-benchmark性能测试工具; 示例: 使用50个并发连接,发出100000个请求,每个 ...

  6. APP前端公共测试点

  7. 2013年5月~2013年11月份(转接关于ns51服务平台项目)相关资料:

    <1> [平台首页] 界面截图:(网络游客所看到的界面首页) <2>[注册] 有需求则注册会员(略...) <3>[个人空间] 注册成功后进入个人空间(有深层次的需 ...

  8. mongoDB中的ID的生成原则

  9. ajax学习笔记(原生js的ajax)

    ajax是一个与服务器端语言无关的技术,可以使用在任何语言环境下的web项目(如JSP,PHP,ASP等). ajax优点: 1) 页面无刷新的动态数据交互 2) 局部刷新页面 3) 界面的美观 4) ...

  10. hdu 2256 好神奇的矩阵!

    这题自己一开始硬是不会处理√6 前面的系数,直到看了别人的博客后才知道是怎么解得,不多说,先付上一张图: 推出这个关系后,就很容易了. #include<cstdio> #include& ...