Skip to content

Commit

Permalink
Merge pull request vprusso#6 from mmohamedali/fix-buy-sell-stock-code
Browse files Browse the repository at this point in the history
Fixed Runtime Error that was causing the code to not run.
  • Loading branch information
vprusso authored Oct 16, 2019
2 parents cb69f7d + 6738586 commit e1009c4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions data_structures/arrays/buy_and_sell_stock_once.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ def buy_and_sell_once(A):
# Time Complexity: O(n)
# Space Complexity: O(1)
def buy_and_sell_stock_once(prices):
max_profit = 0.0
min_price = float('inf')

for price in prices:
min_price = min(min_price, price)

# Check if the prices array is less than 2
if len(prices) < 2:
return 0

min_price = prices[0]
max_profit = 0

for price in prices:
min_price = min(min_price, price)
compare_profit = price - min_price

max_profit = max(max_profit, compare_profit)

return max_profit


Expand Down

0 comments on commit e1009c4

Please sign in to comment.