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. Some Useful Property Settings Explained Of Oracle Forms

    In Oracle forms when we have two or more blocks and there is a requirement to join them or make a re ...

  2. T-SQL利用Case When Then多条件判断

    CASE    WHEN 条件1 THEN 结果1    WHEN 条件2 THEN 结果2    WHEN 条件3 THEN 结果3    WHEN 条件4 THEN 结果4.........    ...

  3. 加快Win7整体运行速度的12个小技巧

    在整体运行速度方面,微软Windows 7系统超越了它的前任Vista,拥有明显的提升;但是相比最新的Windows 8,似乎又有所不及,至少很少有Windows用户能够体会到15秒的开机速度.虽然如 ...

  4. E2 2014.5.8 更新日志

    增加功能 增加报价单功能,可以针对指定客户生成报价单,可以直接生成一个在线地址,直接把地址发给客户在线打开 传统的报价,先生成一个EXCEL,再传给客户,使用E2,这一切都变得简单,你可生成一个在线地 ...

  5. iOS - Swift String 字符串

    前言 public struct String public class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCodin ...

  6. Doragon Kuesuto 1.15

    #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { ; ; ; int actio ...

  7. 通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  8. 关于level DB的相关资料

    可以参考: http://blog.csdn.net/houzengjiang/article/details/7718548 http://www.cnblogs.com/haippy/archiv ...

  9. ios8中,相册创建后手动删除,不能再进行创建显示

    // Add a new ALAssetsGroup to the library. // The name of the ALAssetsGroup is name and the type is ...

  10. grub4dos通用菜单及相关工具包

    grub4dos通用菜单及相关工具包 全套工具包(含PE.ISO,可根据需要替换删减):http://pan.baidu.com/s/1i4EjWod模板文件3.3M(不含PE.ISO):http:/ ...