您的位置:首页 > 编程语言 > C语言/C++

【LeetCode从零单刷】Best Time to Buy and Sell Stock I, II, With Cooldown

2015-04-08 09:16 435 查看
[b]I 题目:[/b]

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.
解答:

因为只能买卖一次,所以需要找到最高价与最低价之间的差距最大的一次,就是答案。

但是这里有一个限制条件,最高价需要在最低价之后再出现,否则还没最低价买入就要最高价卖出不符合现实。

可以利用一个新数组 submin,记录从开始到现在的最低价是多少。然后再利用一个变量max,记录当前价格与当前最低价之间的差距最大是多少。

class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() < 2)   return 0;

int* submin = new int[prices.size()];
submin[1]   = prices[0];
for(int i=2; i<prices.size(); i++)
submin[i] = (prices[i-1] < submin[i-1]) ? prices[i-1] : submin[i-1];

int max = 0;
for(int i=1; i<prices.size(); i++)
max = (max < (prices[i] - submin[i])) ? (prices[i] - submin[i]) : max;
return max;
}
};


II 题目:

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

解答:

开始看到题目就没懂,以为有很多 stock 可以选择。还想到什么在最小利润的那天买入,最大利润的那天卖出之类的,还要保证最小利润在最大利润之前。要不要DP之类。

其实是只有一个stock,而且每天买进卖出的次数还不限制

如果出现这种情况:[1, 2, 3] 第一天买入,第二天卖出,第三天就没东西了,也就没有利益最大化。但是这么想:第一天买入,第二天卖出,然后发现第三天价格更高,可以视为第二天在卖出之后又买入了,这样收益就是(2-1)+(3-2)=(3-1)与第一天买入第三天卖出是一样的。

多此一举的操作大大简化了思路,将整体的关系化解耦成了前后两天的关系。只要前一天价格小于下一天就求差。

注意空集……这个又让我错了一次(总吃这个亏!)。如果对 vector 不了解的可以看看这个:Vector (STL)

class Solution {
public:
int maxProfit(vector<int> &prices) {
int profit = 0;
if(prices.size() == 0) return 0;
for(int i = 0; i< prices.size() - 1; i++)
{
if(prices[i] < prices[i+1])
{
profit += (prices[i+1] - prices[i]);
}
}
return profit;
}
};

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

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]


解答:

讲解参见:[LeetCode]Best Time to Buy and Sell Stock with Cooldown 作者:在线疯狂

注意点有如下:

使用两组 DP 数组,不仅使用一个;

定义数组是单独某天最优值行为,或者是范围内最优值行为。定义一定要明确,否则状态方程会混乱。同时会影响最终求全局最优值的方法:如果每次都是局部最优值,最后需要多一步全局比较
如果前一天进行了sell,第二天不能够buy。但是可以sell,因为视为前一天不sell而是在第二天sell;

不论 “buys[i]表示在第 i 天买入股票所能获得的最大累积收益”,还是 “buys[i]表示在第i天持有股票所能获得的最大累计收益”。第一天的 buys[0] = -prices[0](买入=亏损);

class Solution {
public:
int maxProfit(vector<int>& prices) {
vector<int> sell(prices);
vector<int> buy(prices);

int size = prices.size();
if(size <= 0)   return 0;

sell[0] = 0;
buy[0] = -prices[0];
int delta, tmp1, tmp2, tmp3, tmp4;
for(int i = 1; i < size; i++)
{
delta = prices[i] - prices[i - 1];
tmp1 = buy[i - 1] + prices[i];
tmp2 = sell[i- 1] + delta;
sell[i] = tmp1 > tmp2 ? tmp1 : tmp2;

tmp3 = buy[i - 1] - delta;
tmp4 = sell[i -2] - prices[i];
buy[i] = tmp3 > tmp4 ? tmp3 : tmp4;
}

int max = -1;
for(int i = 0; i< size; i++) max = (sell[i] > max)? sell[i] : max;
return max;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode c++ with cooldown