Day60 買賣股票的最佳時(shí)機(jī)II

給定一個(gè)數(shù)組,它的第 i 個(gè)元素是一支給定股票第 i 天的價(jià)格。設(shè)計(jì)一個(gè)算法來計(jì)算你所能獲取的最大利潤。你可以盡可能地完成更多的交易(多次買賣一支股票)

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/

示例1:

輸入: [7,1,5,3,6,4]
輸出: 7
解釋: 在第 2 天(股票價(jià)格 = 1)的時(shí)候買入,在第 3 天(股票價(jià)格 = 5)的時(shí)候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
隨后,在第 4 天(股票價(jià)格 = 3)的時(shí)候買入,在第 5 天(股票價(jià)格 = 6)的時(shí)候賣出, 這筆交易所能獲得利潤 = 6-3 = 3 。

示例2:

輸入: [1,2,3,4,5]
輸出: 4
解釋: 在第 1 天(股票價(jià)格 = 1)的時(shí)候買入,在第 5 天 (股票價(jià)格 = 5)的時(shí)候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
注意你不能在第 1 天和第 2 天接連購買股票,之后再將它們賣出。
因?yàn)檫@樣屬于同時(shí)參與了多筆交易,你必須在再次購買前出售掉之前的股票。

實(shí)例3:

輸入: [7,6,4,3,1]
輸出: 0
解釋: 在這種情況下, 沒有交易完成, 所以最大利潤為 0。

提示:

1 <= prices.length <= 3 * 10 ^ 4
0 <= prices[i] <= 10 ^ 4

Java解法

思路:

  • 實(shí)際上昨天的解法上對(duì)賣出方式的處理變化,大體一致
  • 在這里需要截取每一段上升趨勢(shì),一旦下降就進(jìn)行賣出
package sj.shimmer.algorithm.m3_2021;

/**
 * Created by SJ on 2021/3/27.
 */

class D60 {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
        System.out.println(maxProfit(new int[]{1,2,3,4,5}));
        System.out.println(maxProfit(new int[]{7, 6, 4, 3, 1}));
    }


    public static int maxProfit(int[] prices) {
        int result = 0;
        if (prices != null && prices.length > 1) {
            int length = prices.length;
            int in = 0;
            for (int i = 1; i < length; i++) {
                if (prices[i] < prices[i-1]) {
                    result+=prices[i-1]-prices[in];
                    in = i;
                }
            }
            result+=prices[length-1]-prices[in];
        }
        return result;
    }
}
image

官方解

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/solution/mai-mai-gu-piao-de-zui-jia-shi-ji-ii-by-leetcode-s/

  1. 動(dòng)態(tài)規(guī)劃

    i天的收益就看第i-1天是否持有,通過這樣的關(guān)系列出動(dòng)態(tài)方程,寫出代碼

    class Solution {
        public int maxProfit(int[] prices) {
            int n = prices.length;
            int[][] dp = new int[n][2];
            dp[0][0] = 0;
            dp[0][1] = -prices[0];
            for (int i = 1; i < n; ++i) {
                dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
                dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
            }
            return dp[n - 1][0];
        }
    }
    

    雖然看起來麻煩了些,但是是使用動(dòng)態(tài)規(guī)劃解決的好例子

    • 時(shí)間復(fù)雜度:O(n)
    • 空間復(fù)雜度:O(1)
  2. 貪心

    類似于我的處理,但計(jì)算了每個(gè)價(jià)格的區(qū)間,去掉負(fù)數(shù)

    class Solution {
        public int maxProfit(int[] prices) {
            int ans = 0;
            int n = prices.length;
            for (int i = 1; i < n; ++i) {
                ans += Math.max(0, prices[i] - prices[i - 1]);
            }
            return ans;
        }
    }
    
    • 時(shí)間復(fù)雜度:O(n)
    • 空間復(fù)雜度:O(1)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容