Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Buy And Sell Stock 2

# 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.
# Note the problem is confusing - given per day prices, you make no profit buying
# and selling on the same day! 

def total_profit(prices: List[UInt]) -> UInt:
    var total_profit: UInt = 0  # Stores the maximum profit found so far

    # Loop until sell day reaches the end of the price list
    for day in range(1, len(prices)):
        if prices[day - 1] < prices[day]:
            # If selling is profitable, sell it & add up profit
            total_profit += prices[day] - prices[day - 1]
    return total_profit  # Return the highest profit found


def main():
    # First test: Best profit is buying at 1 and selling at 6 => profit = 5
    prices = [7, 1, 5, 3, 6, 4]
    debug_assert(total_profit(prices) == 7, "Assertion failed")

    # Second test: No profitable day to sell => profit = 0
    prices = [7, 6, 4, 3, 1]
    debug_assert(total_profit(prices) == 0, "Assertion failed")
    # 3rd test: Best profit is buying at every day and selling next day
    prices = [1, 2, 3, 4, 5]
    debug_assert(total_profit(prices) == 4, "Assertion failed")

View source on GitHub