# Best time to buy and sell stock another approach in python

When it comes to finding the best time to buy and sell stock, there are multiple approaches you can take in Python. In this article, we will explore three different solutions to this problem.

## Solution 1: Brute Force

One way to solve this problem is by using a brute force approach. This involves checking every possible combination of buying and selling days and calculating the maximum profit. Here’s how you can implement this solution:

``````
def max_profit(prices):
max_profit = 0
for i in range(len(prices)):
for j in range(i+1, len(prices)):
profit = prices[j] - prices[i]
if profit > max_profit:
max_profit = profit
return max_profit

prices = [7, 1, 5, 3, 6, 4]
print(max_profit(prices))
``````

This solution has a time complexity of O(n^2) since we have nested loops. It checks every possible combination, so it may not be efficient for large datasets.

## Solution 2: One Pass

A more efficient approach is to use a one-pass algorithm. This involves iterating through the prices list and keeping track of the minimum price and maximum profit. Here’s how you can implement this solution:

``````
def max_profit(prices):
min_price = float('inf')
max_profit = 0
for price in prices:
if price < min_price:
min_price = price
elif price - min_price > max_profit:
max_profit = price - min_price
return max_profit

prices = [7, 1, 5, 3, 6, 4]
print(max_profit(prices))
``````

This solution has a time complexity of O(n) since we only iterate through the prices list once. It is more efficient than the brute force approach and can handle larger datasets.

## Solution 3: Dynamic Programming

Another approach is to use dynamic programming to solve this problem. We can create an array to store the maximum profit at each day. Here’s how you can implement this solution:

``````
def max_profit(prices):
if len(prices) < 2:
return 0
max_profit = [0] * len(prices)
min_price = prices[0]
for i in range(1, len(prices)):
min_price = min(min_price, prices[i])
max_profit[i] = max(max_profit[i-1], prices[i] - min_price)
return max_profit[-1]

prices = [7, 1, 5, 3, 6, 4]
print(max_profit(prices))
``````

This solution also has a time complexity of O(n) since we iterate through the prices list once. It is a more advanced approach that can handle larger datasets and is useful when solving similar problems.

After analyzing the three solutions, the best option depends on the specific requirements of your problem. If you have a small dataset, the brute force approach may be sufficient. However, if you are dealing with larger datasets, the one-pass or dynamic programming approach would be more efficient. Overall, the one-pass algorithm is a good balance between simplicity and efficiency.

Rate this post

### 3 Responses

1. Cohen says:

Solution 2 seems cool, but why not mix all three solutions for maximum profit? 🤔💰

2. Alicia Berger says:

Solution 1: Brute Force – Nah, too slow for my liking. I need speed!

Solution 2: One Pass – Now were talking! Efficient and effective, just the way I like it.

Solution 3: Dynamic Programming – Hmm, sounds fancy. But is it really necessary?

3. Santiago Juarez says:

I cant believe Solution 2 actually works! Dynamic programming for the win! #MindBlown