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 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]

一刷
題解:
buy[i] means before day i what is the maxProfit for any sequence end with buy.
sell[i] means before day i what is the maxProfit for any sequence end with sell.
rest[i] means before day i what is the maxProfit for any sequence end with rest.
Then we want to deduce the transition functions for buy sell and rest. By definition we have:

buy[i]  = max(rest[i-1]-price, buy[i-1]) 
sell[i] = max(buy[i-1]+price, sell[i-1])
rest[i] = max(sell[i-1], buy[i-1], rest[i-1])

由于buy[i] <= rest[i] <= sell[i] is also true, 因此:

buy[i] = max(sell[i-2]-price, buy[i-1])
sell[i] = max(buy[i-1]+price, sell[i-1])
class Solution {
    public int maxProfit(int[] prices) {
        //sell: sell[i-1], prev_sell: sell[i-2]
        int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy;
        for(int price : prices){
            prev_buy = buy;
            buy = Math.max(prev_sell - price, prev_buy);
            prev_sell = sell;
            sell = Math.max(prev_buy + price, sell);
        }
        return sell;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌炮障,老刑警劉巖骨杂,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件流码,死亡現(xiàn)場離奇詭異裸扶,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)确丢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門绷耍,熙熙樓的掌柜王于貴愁眉苦臉地迎上來富俄,“玉大人弹沽,你說我怎么就攤上這事估蹄⊥蓿” “怎么了方椎?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵斩芭,是天一觀的道長纠亚。 經(jīng)常有香客問我歉甚,道長赘阀,這世上最難降的妖魔是什么益缠? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮基公,結(jié)果婚禮上幅慌,老公的妹妹穿的比我還像新娘。我一直安慰自己轰豆,他們只是感情好胰伍,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著酸休,像睡著了一般骂租。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上斑司,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天渗饮,我揣著相機(jī)與錄音,去河邊找鬼宿刮。 笑死互站,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的僵缺。 我是一名探鬼主播胡桃,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼磕潮!你這毒婦竟也來了标捺?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤揉抵,失蹤者是張志新(化名)和其女友劉穎亡容,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體冤今,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡闺兢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片屋谭。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡脚囊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出桐磁,到底是詐尸還是另有隱情悔耘,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布我擂,位于F島的核電站衬以,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏校摩。R本人自食惡果不足惜看峻,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望衙吩。 院中可真熱鬧互妓,春花似錦、人聲如沸坤塞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽摹芙。三九已至灼狰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瘫辩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工坛悉, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留伐厌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓裸影,卻偏偏與公主長得像挣轨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子轩猩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容