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

来源:互联网 发布:华为网络机顶盒密码 编辑:程序博客网 时间:2024/06/04 19:35
#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 = 3transactions = [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 <= idp[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输入:51 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天未持股利润为0vector< 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