1. 程式人生 > >leetcode 123. Best Time to Buy and Sell Stock III 解題報告

leetcode 123. Best Time to Buy and Sell Stock III 解題報告

標籤為Hard,動態規劃題目

與一般的股票題目類似,但是最多支援兩次賣出操作,很容易想到多開一個維度。
F[i][j]為第i次操作後第j天賣出時能得到的最大收益
F[i][j]=max{F[i-1][0..j-1]}+prices[j]-min{prices[i]}(i<j)
邊界條件F[-1][0..size]=0

時間複雜度為O(n^2),空間複雜度為O(n),速度相當慢,耗時為1088ms,只快於2.96%的C++程式碼。
實現程式碼:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
vector<int> ans(prices.size()*2,0); vector<int> p(prices.size(),0);//儲存著前i天的最大值 //第一次 int result = 0; for(int i = 0;i<prices.size();i++) { int counting = 0; for(int j = 0;j<i;j++) { counting =
counting>(prices[i]-prices[j])?counting:(prices[i]-prices[j]); } ans[i] = counting; result = result>counting?result:counting; p[i] = result; //cout<<i<<" "<<counting<<endl; } //第二次 for(int i =
prices.size()+1;i<2*prices.size();i++) { int counting = 0; for(int j = prices.size()+1;j<i;j++) { int number = p[j-prices.size()-1]+(prices[i-prices.size()]-prices[j-prices.size()]); counting = counting>number?counting:number; } ans[i] = counting; result = result>counting?result:counting; //cout<<i<<" "<<counting<<endl; } return result; } };

進一步優化

優化1

根據我的定義,有p[i]來表示第一次交易在前i天所取得的最大值。這個值是根據F[0][j]來產生的,而F[0][j]是可以由j的增加來動態產生的,一段時間內的售出最大值可以由前一天的售出最大值計算得到,因為F[0][j] = prices[j]-min{prices[i]},這個最小价格顯然不會隨著j的增大而改變(除非j自己更新了它)。因此會有F[0][j] = max(0,F[0][j-1]+prices[j]-prices[j-1]),此時p[i]的計算代價可以縮小到O(n)。

        int result = 0;
        for(int i = 1;i<prices.size();i++)
        {
            ans[i] = max(ans[i-1]+prices[i]-prices[i-1],0);
            result = result>ans[i]?result:ans[i];
            p[i] = result;
        }

優化後計算時間為588ms,快於4.94%的C++程式碼。

優化2

觀察後面的二層迴圈,可以發現number = p[j-prices.size()-1]+(prices[i-prices.size()]-prices[j-prices.size()]);這條式子可以化為number = p[j-prices.size()-1]-prices[j-prices.size()]+prices[i-prices.size()];,考慮到i是不變的,也就是利潤等於買入前一天的最大值與買入時的股票價格的差值的最大值加上賣出時的股票價格。
因此,設m[j]=max{p[j-1]-prices[j]}F[1][j] = m[j-1]+prices[j]

得到程式:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<int> ans(prices.size()*2,0);
        vector<int> p(prices.size(),0);//儲存著前i天的最大值
        vector<int> m(prices.size(),-1*(1<<31));
        //第一次
        int result = 0;
        for(int i = 1;i<prices.size();i++)
        {
            ans[i] = max(ans[i-1]+prices[i]-prices[i-1],0);
            result = result>ans[i]?result:ans[i];
            p[i] = result;
            m[i] = p[i-1]-prices[i];
            m[i] = m[i]>m[i-1]?m[i]:m[i-1];
            //cout<<"m"<<i<<" "<<m[i]<<endl;
        }
        //第二次
        for(int i = 1;i<prices.size();i++)
        {
            ans[i] = m[i-1]+prices[i];
            result = result>ans[i]?result:ans[i];
        }
        return result;
    }
};

得到的結果為4ms,快於99.66%的C++程式,優化結束。

相關推薦

leetcode 123. Best Time to Buy and Sell Stock III 解題報告

標籤為Hard,動態規劃題目 與一般的股票題目類似,但是最多支援兩次賣出操作,很容易想到多開一個維度。 設F[i][j]為第i次操作後第j天賣出時能得到的最大收益 有F[i][j]=max{F[i-1][0..j-1]}+prices[j]-min{prices

[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 123 :Best Time to Buy and Sell Stock III

   該問題和121,122是一類問題,難度逐層遞增,首先先考慮121題       只能交易一次,尋找最大利潤,一開始想到的最簡單的策略就是用一個值儲存目前為止碰到的最小進價,和當前值相減後與當前最大利潤比較,這樣只需要兩個額外變數即可,但

[leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股時機之三

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 123. Best Time to Buy and Sell Stock III (stock problem)

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 a

leetcode | 123. Best Time to Buy and Sell Stock III

題目 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 123. Best Time to Buy and Sell Stock III

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. Y

Leetcode 123 Best Time to Buy and Sell Stock III 至多兩次買賣股票最大收益

題目描述 Say you have an array for which the ith element is the price of a given stock on day i. 假設你有一個數組,裡面記錄的是每一天的股票的價格。 Desig

LeetCode123.Best Time to Buy and Sell Stock III

題目描述(Hard) 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

#123 Best Time to Buy and Sell Stock III

ret 交易 define find one 之間 sel tor arp /* 找一次操作能獲取最大利潤的一次交易(即題目121)的idxBuy和idxSell,這次交易稱為首次交易 然後分5中情況: 情況1:最優解就只有“首次交易” 情況2:“首次交易”加上左邊的交易

123. Best Time to Buy and Sell Stock III

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

【python/Hard/leetcode/123Best Time to Buy and Sell Stock III

題目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 基本思路 開闢兩個陣列p1和p2 p1[i]表示在price[i]之前進行一次交易所獲得的最大利潤, p2[i]表示在price[i

leetcode123.(Hard)Best Time to Buy and Sell Stock III

解題思路: DP,只是儲存的空間是O(1) 這個討論區的答案 首先buy1點是第一次買入點,對於當前點數,只有一個點數的時候只能買入,有兩個及以上的點數時可以選擇買入當前點數或者不買入當前點數即還是使用之前的點數。 sell1是第一次售出點,對於 當前點數可以選擇售出,或者不售出 buy

LeetCode】#123買賣股票的最佳時機III(Best Time to Buy and Sell Stock III)

【LeetCode】#123買賣股票的最佳時機III(Best Time to Buy and Sell Stock III) 題目描述 給定一個數組,它的第 i 個元素是一支給定的股票在第 i 天的價格。 設計一個演算法來計算你所能獲取的最大利潤。你最多可以完成 兩筆 交易。 注意

leetcode【121+122+123 best time to buy and sell stock】【python】

我們先拿出來前三道題,因為他們都是array中的題目。這是leetcode種經典的一系列題,涉及到動態規劃和貪心演算法。按照我的理解,貪心是滿足當前條件的最優值我們就將它最為最優解,也就是大家說的區域性最優值,而動態規劃是要記錄下來達到當前最優解的所有途徑,由區

[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——Best Time to Buy and Sell Stock III 買賣股票最大收益(AC)

element cti () -- 最大 leetcode price imu cto Say you have an array for which the ith element is the price of a given stock on day i. D

[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