Answer to Question #297486 in Python for Ram

Question #297486

Maximum Profit


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 an 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 input 1

7 1 5 3 6 4

sample output 1

5


sample input 2

1 11 13 21 19

sample output 2

20


1
Expert's answer
2022-02-16T08:10:53-0500
prices = input().split(' ')
prices.reverse()

max = 0;
print(prices)
for i in range(len(prices)-1):
    for j in range(i+1, len(prices)):
        res = int(prices[i]) - int(prices[j])
        if res > max:
            max = res

print(max)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS