Given an array of stock prices for a day, where prices[i] is the price of the stock at time i, write a function that returns the maximum amount of money one could make in a day if he or she buys and sells at most once.
[7,2,1,5,10] -> 9
[10,1,5] -> 4
# First Solution
def stockPrices(prices):
max_profit = 0;
for i in range(0,len(prices)):
current_day = prices[i]
for sell in prices[i:-1]:
if (sell - current_day > max_profit):
max_profit = sell - current_day
return max_profit
[7,2,1,5,10] -> 9
# Second Solution
def stockPrices2(prices):
minBuyPrice = 0;
maxProfit = 0;
for i in range(len(prices)):
if (prices[i] < minBuyPrice):
minBuyPrice = prices[i]
if (prices[i+1] - minBuyPrice > maxProfit):
maxProfit = prices[i+1] - minBuyPrice
return maxProfit;