Skip to content

Latest commit

 

History

History
91 lines (72 loc) · 3.85 KB

122.Best_Time_to_Buy_and_Sell_Stock_II(Medium).md

File metadata and controls

91 lines (72 loc) · 3.85 KB

122. Best Time to Buy and Sell Stock II (Medium)

Date and Time: Jul 6, 2024, 14:43 (EST)

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


Question:

You are given an integer array prices where prices[i] is the price of a given stock on the $i^\text{th}$ 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.


Example 1:

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

Output: 7

Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.

Example 2:

Input: prices = [1, 2, 3, 4, 5]

Output: 4

Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.

Example 3:

Input: prices = [7, 6, 4, 3, 1]

Output: 0

Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.


KeyPoints:

We just greedily add all the profits by comparing current prices[i] with previous prices[i-1], if it is less than the previous price, we can sell it since we can get profits. Note that we should start the for loop from (1, len(prices)), because we want to compare with prices[i-1].

We can also use similar approach in 121. Best Time to Buy and Sell Stock, but we need to set mini = maxi, buy it back when we sell it at this price, so if a greater price appear, we can still earn it greedily.


My Solution:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # Greedily add all profits
        profit = 0
        for i in range(1, len(prices)):
            if prices[i] > prices[i-1]:
                profit += (prices[i] - prices[i-1])
        return profit

Time Complexity: $O(n)$
Space Complexity: $O(1)$


Similar to 121:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # Each price, either update mini or (maxi and profits)
        # Greedy, we can buy it, and a greater one, we can sell, then buy this one back

        # TC: O(n), n=len(prices), SC: O(1)
        mini, maxi = float("inf"), 0
        profits = 0
        for i in range(len(prices)):
            # If less price, we don't buy the previous one
            if prices[i] < mini:
                mini = prices[i]
            else:
                maxi = prices[i]
                profits += maxi - mini
                mini = maxi
        return profits

Time Complexity: $O(n)$
Space Complexity: $O(1)$


CC BY-NC-SABY: credit must be given to the creatorNC: Only noncommercial uses of the work are permittedSA: Adaptations must be shared under the same terms