LeetCode #122 Best Time to Buy and Sell Stock II 買賣股票最好的時機(二)

LeetCode題目翻譯

英文原文如下

You are given an integer array prices where prices[i] is the price of a given stock on the ith day.

On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.

Find and return the maximum profit you can achieve.

中文翻譯

給予你一個陣列 prices ,裡面放的是某支股票一段時間的價格  prices[i] = ith 日的價格

每一天你都可以決定要買股票或賣掉股票,你最多只能持有一股股票不管在什麼時候。不過,你可以在當天馬上買進及賣出。

尋找並返回你可以獲得的最大利潤。

範例及題目限制


解題思路

這一題是【買賣股票】系列文章的第二題 (第一題這裡看 ➡ LeetCode #121 Best Time to Buy and Sell Stock)

與第一題只能買賣股票一次不同的是,這題可以一直進行買賣

我們嘗試的第一個方案是,買在低點賣在高點

具體操作就是,當價格在跌的時候,我們會等到最低點買進

相反的,上漲的時候,我們會等到最高點再賣出

C# 解決方案

方案1

public class Solution {
    public int MaxProfit(int[] prices) {
        int res = 0;
        int buy = 0;

        for(int i = 0; i<prices.Length; i++)
        {
            while(i+1<prices.Length && prices[i]>=prices[i+1])
            {
                i++;
            }

            buy = prices[i];

            while(i+1<prices.Length && prices[i]<prices[i+1])
            {
                i++; 
            }

            res += prices[i]-buy;
        }
        return res;
    }
}

方案2

第二種方式更簡單了,直接判斷後一個數字是否大於前一個數字

如果是的話,就將差值加總起來

public class Solution {
    public int MaxProfit(int[] prices) {
        int res = 0;
        for(int i = 1; i<prices.Length; i++)
        {
            if(prices[i]>prices[i-1])
            {
                res += prices[i]-prices[i-1];
            }
        }
        return res;
    }
}

Java解決方案

方案1

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        int buy = 0;

        for(int i = 0; i<prices.length; i++)
        {
            while(i+1<prices.length && prices[i]>=prices[i+1])
            {
                i++;
            }

            buy = prices[i];

            while(i+1<prices.length && prices[i]<prices[i+1])
            {
                i++; 
            }

            res += prices[i]-buy;
        }
        return res;
    }
}

方案2

class Solution {
    public int maxProfit(int[] prices) {
        int res = 0;
        for(int i = 1; i<prices.length; i++)
        {
            if(prices[i]>prices[i-1])
            {
                res += prices[i]-prices[i-1];
            }
        }
        return res;
    }
}

Python3 解決方案

方案1

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        buy = 0

        i = 0
        while(i<len(prices)):    
        ###for i in range(0,len(prices)):
            while(i+1<len(prices) and prices[i]>=prices[i+1]):
                i+=1
            
            print(i)
            buy = prices[i]

            while(i+1<len(prices) and prices[i]<prices[i+1]):
                i+=1
            
            res += (prices[i]-buy)
            i+=1
        
        return res

方案2

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res = 0
        for i in range(1,len(prices)):
            if(prices[i]>prices[i-1]):
                res += prices[i]-prices[i-1]
        return res

JavaScript 解決方案

方案1

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    var res = 0;
    var buy = 0;

    for(let i = 0; i<prices.length; i++)
    {
        while(i+1<prices.length && prices[i]>=prices[i+1])
        {
            i++;
        }

        buy = prices[i];

        while(i+1<prices.length && prices[i]<prices[i+1])
        {
            i++; 
        }

        res += prices[i]-buy;
    }
    return res;
};

方案2

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function(prices) {
    var res = 0;
    for(let i = 1; i<prices.length; i++)
    {
        if(prices[i]>prices[i-1])
        {
            res += prices[i]-prices[i-1];
        }
    }
    return res;
};

結論

要是買賣股票就跟寫這題一樣簡單就好了嗚嗚…

🧡如果這篇文章有幫上你的一點點忙,那是我的榮幸

🧡收藏文章或幫我分享,我都會很感謝的

✅如有任何疑問,歡迎透過留言或messenger讓我知道 !

題目連結 : Best Time to Buy and Sell Stock II – LeetCode

一些隨機文章

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *