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. jquery之each()、$.each()[jQuery.each()]

    导航: 1,jQuery对象(实例)的each()函数 2,全局jQuery对象的each()函数 一:jQuery对象(实例)的each()函数 each()函数用于以当前jQuery对象匹配到的每 ...

  2. 数独Sudoku

    数独(すうどく,Sūdoku),是源自18世纪瑞士发明,流传到美国,再由日本发扬光大的一种数学游戏.是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并 ...

  3. 5.4.1 termios结构,关闭回显功能,一键入字符fgetc立刻返回,不用按下回车键

    Linux提供了一组编程接口,用来控制终端驱动程序的行为.这样我们可以更精细的来控制终端. 例如: 回显:允许控制字符的回显,例如读取密码时. 使用termios结构的密码程序 #include &l ...

  4. Python基础学习笔记(一)入门

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-chinese-encoding.html 3. http://w ...

  5. web工程中各类地址写法的总结

    首先打一个"/" //如果地址给服务器用,那么"/"就代表该web应用 , 如果给浏览器用的,那么"/"就代表网站(其下有多个web应用) ...

  6. Yii2文件上传

    首先在app\controllers下建立TestController.php,内容为如下代码: <?php namespace app\controllers; use Yii; use yi ...

  7. [转载] Linux下高并发socket最大连接数所受的各种限制

    原文: http://mp.weixin.qq.com/s?__biz=MzAwNjMxNjQzNA==&mid=207772333&idx=1&sn=cfc8aadb422f ...

  8. poj3334Connected Gheeves(二分)

    链接 二分高度,算面积的地方有点麻烦,没有用求交点的模板,直接自己按三角形相似手算了一下,写的有点麻烦. 上下界直接取水可放的最高点以及最低点. 自己的长得很挫的代码 #include <ios ...

  9. C++——将成员函数作为参数

    在C++中,成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的,首先 我们来回顾一下普通函数指针的传递方法: //------------------------------------- ...

  10. order by 容易出现的bug记录

    写分页查询时遇到一个问题: 在order by create_time 的时候,假设所有数据的create_time 值相同,那么 使用:select * from ( selelct s.*,row ...