You given a list of prices where prices[i] is the price of a given stock o the i th day write a program to print the maximum profit by choosing a single day to buy a stock and choosing a different day in the future to sell that stock if these is no profit that can be achieved return 0.
Input
The input is a single line containing space seperated integers.
Output
The output should be a single line integer.
Explanation
In the example the given prices are 7 1 5 3 6 4.
buying stocks on day two having price 1 and selling them on the fifth day having price 6 would give the maximum profit which is 6 - 1
so the output should be 5.
Sample Input1
1 2 1 3 6 7 4
Sample Output1
6
def profit(prices):
maxProfit = 0
if len(prices) > 2:
for i in range(len(prices)-1):
for j in range(i+1, len(prices)):
if prices[j] - prices[i] > maxProfit:
maxProfit = prices[j] - prices[i]
return maxProfit
prices = [int(i) for i in input().split()]
print(profit(prices))
Comments
Leave a comment