麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

309. Best Time to Buy and Sell Stock with Cooldown -Medium

2019-11-10 19:16:36
字體:
來源:轉載
供稿:網(wǎng)友

Question

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)

現(xiàn)在給你一個數(shù)組,第i個元素代表第i天的股票價格。設計出一個算法去找到最大的收益。允許多次交易,但是你必須在第二次買股票前賣掉股票,而且當你賣掉股票,第二天你不被允許買股票(冷卻時間)

Example

prices = [1, 2, 3, 0, 2] maxProfit = 3 transactions = [buy, sell, cooldown, buy, sell]

Solution

動態(tài)規(guī)劃解。思考問題仍然從如何找到第i天和前一天的最大收益的關系出發(fā)。我們可以發(fā)現(xiàn)每一天都有三種狀態(tài),即買(buy),賣(sell),不買不賣(rest),那么其實我們可以分別記錄第i天buy, sell, rest的最大收益記錄下來,然后根據(jù)它們之間的關系更新。這個思路完全沒有問題,但是在實際操作的時候我們會發(fā)現(xiàn),rest其實也要分兩種情況。

第一種是sell后,buy前的rest,因為sell后不能馬上buy,所以如果第i天是rest_before_buy,那么第i - 1天只能是rest_before_buy本身或者sell第二種是buy后,sell前的rest,如果第i天是rest_after_buy,那么第i - 1天只能是rest_after_buy或者buy

所以這兩種rest并不是同一種,因此其實每一天有四種狀態(tài),即rest_before_buy, buy, rest_after_buysell, sell,關系如下圖

關系圖

關系式為:

buy[i] = rest_before_buy[i - 1] - prices[i]

rest_after_buy[i] = max(buy[i - 1], rest_after_buy[i - 1])

sell[i] = max(buy[i - 1] + prices[i], rest_after_buy[i - 1] + prices[i])

rest_before_buy[i] = max(sell[i - 1], rest_before_buy[i - 1])

初始化為:

buy[0] = -prices[0] # 第一天買進花費prices[0]

rest_after_buy[0] = -prices[0] # 因為是不買不賣,所以和buy[0]相同

sell[0] = - (prices[0] + 1) # 第一天不可能賣的,所以設一個非常小的值

rest_before_buy = 0 # 第一天如果不買不賣,花費0

代碼

class Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int """ if len(prices) == 0: return 0 sell = [0] * len(prices) buy = [0] * len(prices) rest_before_buy = [0] * len(prices) rest_after_buy = [0] * len(prices) # 初始化 buy[0] = - prices[0] sell[0] = - (prices[0] + 1) rest_after_buy[0] = - prices[0] for index_p in range(1, len(prices)): buy[index_p] = rest_before_buy[index_p - 1] - prices[index_p] rest_after_buy[index_p] = max(buy[index_p - 1], rest_after_buy[index_p - 1]) sell[index_p] = max(rest_after_buy[index_p - 1] + prices[index_p], buy[index_p - 1] + prices[index_p]) rest_before_buy[index_p] = max(sell[index_p - 1], rest_before_buy[index_p - 1]) # 因為最后一天buy或者rest_after_buy(有股票沒賣掉)都不可能利益最大的,所以不需比較 return max(sell[-1], rest_before_buy[-1])
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 在线2区 | 精国产品一区二区三区 | 欧美激情综合网 | 在线成人一区二区 | 国产99精品 | 妇女毛片| 亚洲极色 | 黄色片快播 | 成人在线视频黄色 | 免费毛片观看 | 国产精品探花在线观看 | 国语自产免费精品视频在 | 中文字幕在线观看1 | 国产亚洲欧美在线视频 | 久久精品二区 | 免费色片 | 国产精品一区在线观看 | 成人不卡在线观看 | 久久久激情网 | 羞羞视频一区 | 一级黄色影院 | 一级视频网站 | 欧美日韩一区,二区,三区,久久精品 | 一级美女大片 | 黄色大片在线免费观看 | 日韩欧美精品电影 | 全黄裸片武则天一级第4季 偿还电影免费看 | 精品久久久久久久久久久aⅴ | 成人男女激情免费视频 | 国产精品剧情一区二区三区 | 色视频在线 | h视频在线免费看 | 羞羞视频.www在线观看 | 91精品国产一区二区在线观看 | 亚洲日韩中文字幕一区 | 免费a级网站 | 国产精品九九久久一区hh | av人人| 999精品久久久 | 国产精品成人免费一区久久羞羞 | 中文字幕在线第二页 |