快轉到主要內容
leetcode 121 - Best Time to Buy and Sell Stock

leetcode 121 - Best Time to Buy and Sell Stock

·
類別 
Leetcode
標籤 
Java Leetcode Easy Array Problem Blind75
Eason Chiu
作者
Eason Chiu
一個不做筆記就容易忘記的工程師
目錄

題目
#

leetcode 121 - Best Time to Buy and Sell Stock (題目說明請點連結)

題目簡述
#

找出買入和賣出股票的最佳時機,使得利潤最大化。

範例
#

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: 在第2天買入(價格 = 1),在第5天賣出(價格 = 6),利潤 = 6-1 = 5。
注意:不能在買入前賣出股票。

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: 在這種情況下,沒有交易完成,所以最大利潤為 0。

Example 3:

Input: prices = [3,2,6,5,0,3]
Output: 4
Explanation: 在第2天買入(價格 = 2),在第3天賣出(價格 = 6),利潤 = 6-2 = 4。

解題思路
#

這題要求我們找出買入和賣出股票的最佳時機,使得利潤最大化。我們需要找到價格陣列中的最小值和最大值,但必須在最小值之後出現最大值。

解法
#

貪心法(Greedy Approach)

  1. 維護一個變數 buy 記錄當前遇到的最低買入價格
  2. 維護一個變數 profit 記錄當前最大利潤
  3. 遍歷價格陣列,更新最低買入價格和最大利潤

步驟
#

  • 初始化 buy = prices[0](假設第一天買入)和 profit = 0
  • 從第二天開始遍歷價格陣列:
    • 如果當前價格比 buy 低,更新 buy 為當前價格
    • 計算當前價格與 buy 的差值,更新 profit 為最大值
  • 最終返回 profit

核心邏輯
#

buy = min(buy, current_price)
profit = max(profit, current_price - buy)

例子說明
#

prices = [7,1,5,3,6,4]

天數當前價格buy操作profit說明
初始化-7-0設定初始買入價格和利潤
第2天11更新buy0buy > 1,更新為1;profit = max(0, 1-1) = 0
第3天51不變4buy = 1(不變);profit = max(0, 5-1) = 4
第4天31不變4buy = 1(不變);profit = max(4, 3-1) = 4
第5天61不變5buy = 1(不變);profit = max(4, 6-1) = 5
第6天41不變5buy = 1(不變);profit = max(5, 4-1) = 5
最終結果-1-5最大利潤為5

完整程式碼
#

java
class Solution {
    public int maxProfit(int[] prices) {
        // 初始化買入價格為第一天的價格
        int buy = prices[0];
        // 初始化最大利潤為0
        int profit = 0;

        // 從第二天開始遍歷價格陣列
        for(int i = 1; i < prices.length; i++){
            // 如果當前價格比買入價格低,更新買入價格
            if(buy > prices[i]){
                buy = prices[i];
            }

            // 計算當前價格與買入價格的差值,更新最大利潤
            profit = Math.max(profit, prices[i] - buy);
        }

        // 返回最大利潤 
        return profit;
    }
}

時間複雜度
#

  • 時間複雜度O(n),其中 n 是價格陣列的長度,只需要遍歷一次陣列
  • 空間複雜度O(1),只使用了常數個變數來存儲狀態

相關文章

leetcode 252 - Meeting Rooms
類別 
Leetcode
標籤 
Java Leetcode Easy Blind75
leetcode 15 - 3Sum
類別 
Leetcode
標籤 
Java Leetcode Medium Two Pointers Problem Blind75
leetcode 344 - Rrevese String
類別 
Leetcode
標籤 
Java Leetcode Easy Two Pointers Problem