1. 程式人生 > >LeetCode #309 Best Time to Buy and Sell Stock with Cooldown

LeetCode #309 Best Time to Buy and Sell Stock with Cooldown

Problem Description

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]

Some Details

  • 標準的簡單DP題目

Solution

DP沒怎麼切,這題雖然難度不高但是既然切到了就趕快寫了,畢竟作業難以完成= =。
買入,賣出,冷卻,三狀態,ok
f[i][0] 第i天手上沒貨的最優獲利
f[i][1] 第i天手上有貨的最優獲利
f[i][2] 第i天進入冷卻的最優獲利
轉移方程:
f[i][0]=max(f[i-1][0],f[i-1][2])
//前一天就沒貨或者前一天冷卻
f[i][1]=max(f[i-1][0]-price[i],f[i-1][1])
//前一天就有貨或者今天買入
f[i][2]=f[i-1][1]+price[i];
//前一天有貨今天賣了
結束思密達。

Code

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int f[10100][4],siz=prices.size();
        if (siz==0) return 0;
        f[0][0]=0;
        f[0][1]=prices[0]*-1;
        f[0][2]=0;
        for (int i=1;i<siz;i++)
        {
            f[i][0]=max(f[i-1][0],f[i-1][2]);
            f[i][1]=max(f[i-1][0]-prices[i],f[i-1][1]);
            f[i][2]=f[i-1][1]+prices[i];
        }
        int ans=f[siz-1][0];
        ans=max(ans,f[siz-1][1]);
        ans=max(ans,f[siz-1][2]);
        return ans;
    }
};

相關推薦

LeetCode:309. Best Time to Buy and Sell Stock with Cooldown

買賣股票系列的第5題。題目是: 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 m

LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

題解 注意這道dp題對選擇和狀態的描述。 用buy sell rest, 三個陣列分別表示在 第i天時 能獲得的最大收益。 有轉移方程: buy[i] = max( rest[i

[LeetCode 309] Best Time to Buy and Sell Stock with Cooldown(動態規劃及進一步優化)

題目內容 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 st

LeetCode #309 Best Time to Buy and Sell Stock with Cooldown

Problem Description Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm t

LeetCode309. Best Time to Buy and Sell Stock with Cooldown

309. Best Time to Buy and Sell Stock with Cooldown Description: Say you have an array for which the  element is the price of a given s

LeetCode 714. Best Time to Buy and Sell Stock with Transaction Fee

題解型別 這裡有一篇極盡完美的足以秒殺所有 stock buy&sell 型別的題解,寫的非常完善。 給出我覺得重要的點: T[ i, k, b ] 表示到i位為止交易k次情況下 是否(b=0 or 1)持有股票時獲得最大收益 邊界條件 T[-1][

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

Best Time to Buy and Sell Stock with Cooldown

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

[Swift]LeetCode309. 最佳買賣股票時機含冷凍期 | 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

[LeetCode] 121. Best Time to Buy and Sell Stock Java

most length 如果 時間復雜度 ase 最大差值 new [1] cas 題目: Say you have an array for which the ith element is the price of a given stock on day i.

[LeetCode] 22. Best Time to Buy and Sell Stock II Java

股票 log ive highlight transacti ever 方法 size 可能 題目: Say you have an array for which the ith element is the price of a given stock on day i

leetcode 121. Best Time to Buy and Sell Stock

clas which style ces pre max des sign har Say you have an array for which the ith element is the price of a given stock on day i. If you

[leetcode] 121. Best Time to Buy and Sell Stock 解題報告

leet 解題報告 情況 lee 動態規劃 turn fit mat etc 動態規劃,註意處理當前最大值小於0的情況 public int maxProfit(int[] prices) { if (prices == null || prices.le

Leetcode 122: Best Time to Buy and Sell Stock II

com same clas multipl fit sel fin ans let Say you have an array for which the ith element is the price of a given stock on day i. Design

Leetcode 121: Best Time to Buy and Sell Stock

output inpu return ive price interview not don style Say you have an array for which the ith element is the price of a given stock on da

[LeetCode] 122. Best Time to Buy and Sell Stock II 買賣股票的最佳時間 II

complete design fit sha 一個 tran 利潤 多個 mes Say you have an array for which the ith element is the price of a given stock on day i. Design

[LeetCode] 188. Best Time to Buy and Sell Stock IV 買賣股票的最佳時間 IV

股票 ice not ++i int ase which before oba Say you have an array for which the ith element is the price of a given stock on day i. Design a

[LeetCode] 123. Best Time to Buy and Sell Stock III 買賣股票的最佳時間 III

AC range ger AR sign self you www release Say you have an array for which the ith element is the price of a given stock on day i. Design

[LeetCode] 121. Best Time to Buy and Sell Stock 買賣股票的最佳時間

IT diff ger cti 利潤 變量 lee algorithm clas Say you have an array for which the ith element is the price of a given stock on day i. If you

LeetCode-122. Best Time to Buy and Sell Stock II

0.原題 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 maximu