LeetCode Problem 121. Best Time to Buy and Sell Stock Solution in Python

This is a solution for the coding problem 121. Best Time to Buy and Sell Stock from LeetCode in Python:

In this post, I’m going to talk about a fun problem called “Best Time to Buy and Sell Stock.” This problem is all about figuring out how much money we can make by buying and selling a stock at the right times.

Imagine that you have a list of numbers, and each number represents the price of a stock on a different day. Your goal is to find the biggest difference between any two numbers in the list – this difference represents the most money you can make by buying the stock at the lower price and selling it at the higher price.

To solve this problem, we can use a special kind of loop called a “for” loop. This loop will help us go through each number in the list and check if it’s the best time to buy or sell the stock.

First, we’ll create a variable called “min_price” and set it equal to the first number in the list. This variable will help us keep track of the lowest price we’ve seen so far. Then, we’ll create another variable called “max_profit” and set it equal to 0. This variable will help us keep track of the most money we’ve made so far.

Next, we’ll start the for loop. For each number in the list, we’ll do the following:

  • If the current number is less than the “min_price” we’ve seen so far, we’ll update “min_price” to the current number.
  • If the current number is greater than or equal to “min_price,” we’ll calculate the potential profit by subtracting “min_price” from the current number. Then, we’ll compare this potential profit to the current “max_profit” – if it’s bigger, we’ll update “max_profit” to the new potential profit.

After we’ve gone through every number in the list, we’ll return “max_profit” as our answer. This will be the most money we can make by buying and selling the stock at the right times.

That’s it! We’ve just solved the “Best Time to Buy and Sell Stock” problem.

Now, let’s talk about the time and space complexity of our solution. Time complexity is a measure of how long it takes for our solution to finish running, and space complexity is a measure of how much memory our solution uses.

In this case, our solution has a time complexity of O(n), which means it will take about the same amount of time to run no matter how big the input list is. This is because we only have to go through the list once, and we do a constant number of operations on each element.

Our solution also has a space complexity of O(1), which means it only uses a fixed number of variables no matter how big the input list is. This is because we only use two variables – “min_price” and “max_profit” – to solve the problem.

I hope you enjoyed learning about this problem and how to solve it with Python. Keep practicing your coding skills and you’ll be a pro in no time!

Here is the complete code:

def maxProfit(prices):
    # If the input list is empty, return 0
    if not prices:
        return 0

    # Set the minimum price to the first element in the list
    min_price = prices[0]
    # Set the maximum profit to 0
    max_profit = 0

    # Iterate through the list of prices
    for price in prices:
        # If the current price is less than the minimum price seen so far, update the minimum price
        if price < min_price:
            min_price = price
        # If the current price is greater than or equal to the minimum price, calculate the potential profit and update the maximum profit if necessary
        else:
            max_profit = max(max_profit, price - min_price)

    # Return the maximum profit
    return max_profit

Happy coding!

By Amin Beheshti

Software Engineer & Content Creator

Leave a comment

Your email address will not be published. Required fields are marked *