leecode 解题总结:309. Best Time to Buy and Sell Stock with Cooldown

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
/*
问题:
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]

分析:此题仍然是购买股票问题。但是抛售股票的下一天不允许买入。
求可以获得的最大利润。
也就是说之前每一次抛售都对后续利润是有影响的
没有限制交易次数,之前限制交易次数是用的dp方法。
当时设置dp[i][k]表示截止到第i天交易k次获取的最大利润
dp[i][k] = max(dp[i-1][k] , dp[j][k-1] + prices[i] - prices[j]),其中j属于[0,i-1]
无限制交易次数原本就是累加所有两天差值中的正数
加入cooldown带来的影响是那一天和前面一天的差值,以及和后面一天的差值不能累加
如果出现连续递增序列,不要分开多次买入和卖出,集中在一次里面
暴力破解:罗列可以尝试购买的天数。
一定需要记录抛售的那一天
如果从后向前,最后
dp[i][k]表示截止到第i天,在第k天抛售后获取的最大利润 ,其中0 <= k <= i
dp[i][k] = max(dp[i-1][k],dp[i][j] + prices[i] - prices[j+1]) ,  0 <= j < k
表示截止到第i天,在第k天抛售获取的最大利润= 截止到第i天,在第j天抛售获取的最大利润 + 第i天减去第j+1天获得的利润
与截止到第i-1天,在第k天抛售的最大利润 中的较大值
所求目标是dp数组中的最大值
边界:dp[i][0]=0:截止到第i天,第0天抛售利润为0
     dp[0][k]=0:截止到第0天,在第k天抛售利润为0
	 dp[i][k]=0,if k> i

输入:
5
1 2 3 0 2
输出:
3

参考解法:http://www.phperz.com/article/15/1225/177127.html
最终手里无股票,记录两个状态:持股和未持股
设sell[i]:表示第i天不持股时的最大利润
  buy[i]:表示第i天持股时的最大利润
如果今天未持股,最大利润来源于:
1】今天未持股和昨天一样未持股(sell[i-1])
2】昨天持股(buy[i-1]),今天卖出,卖出当天价格为prices[i],默认
卖出当天的股票,则在原先利润基础上加上当天价格
总利润=昨天持股+今天卖出=buy[i-1] + prices[i]
卖出到底指什么?
sell[i]=max(sell[i-1] , buy[i-1] + prices[i])

如果今天持股,最大利润来源于:
1】今天和昨天一样都持股(buy[i-1])
2】今天买入股票prices[i],昨天冷却,前天卖出股票,即前天未持股(sell[i-2]),
总利润=前天不持有+今天买入=sell[i-2] + prices[i]

关键:
总结:sell[i]表示第i天未持股最大利润,buy[i]表示第i天持股最大利润
当天如果买入股票,利润减少,你只是买,还没有卖出
1】今天未持股的最大利润= 昨天未持股的最大利润 , 昨天持股今天卖出  中两者较大值
			sell[i] = max(sell[i-1] , buy[i-1] + prices[i])
2】今天持股的最大利润= 昨天持股的最大利润 , 前天卖出(未持股)+今天买入  中两者较大值
            buy[i] = max(buy[i-1] , sell[i-2] - prices[i])
			不会前天持股,然后今天买入(因为再次买入前必须卖出)
初始的时候:sell[0]:第0天未持股的最大利润应该是0
           buy[0]:第0天持股的最大利润 -prices[0],因为意味着第0天买入股票
所求的是第n-1天手上没有股票的最大利润sell[n-1]
如何进行更新:buy[i]设计到sell[i-2],因此最好将sell[i]放在前面
buy[1]如何计算? sell[i-2]下标不对,如果 i < 2,sell[i-2]默认为0,第sell[-1]天
利润未持股肯定为0,还没有买入
*/

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.empty())
		{
			return 0;
		}
		int size = prices.size();
		//sell[i]=max(sell[i-1] , buy[i-1] + prices[i])
		//sell[0] = 0:第0天未持股利润为0
		vector< int > sell(size , 0);
		//buy[i] = max(buy[i-1] , sell[i-2] - prices[i])
		//buy[0]:第0天持股,等于买入,利润为-prices[i]
		vector< int > buy(size , 0);
		buy.at(0) = -prices.at(0);
		int profit = 0;
		for(int i = 1 ; i < size ; i++)
		{
			sell[i] = max(sell[i-1] , buy[i-1] + prices[i]);
			if(i >= 2)
			{
				buy[i] = max(buy[i-1] , sell[i-2] - prices[i]);
			}
			else
			{
				buy[i] = max(buy[i-1] , -prices[i]);
			}
		}
		return sell[size - 1];
    }
};

void print(vector<int>& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	for(int i = 0 ; i < size ; i++)
	{
		cout << result.at(i) << " " ;
	}
	cout << endl;
}

void process()
{
	 vector<int> nums;
	 int value;
	 int num;
	 Solution solution;
	 int result;
	 while(cin >> num )
	 {
		 nums.clear();
		 for(int i = 0 ; i < num ; i++)
		 {
			 cin >> value;
			 nums.push_back(value);
		 }
		 result = solution.maxProfit(nums);
		 cout << result << endl;
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值