Best Time to Buy and Sell Stock
Medium 40 XPArraysGreedy
Commonly asked at Amazon, Google, Microsoft, Facebook
Read a list of daily stock prices. You may buy once and sell once on a later day. Print the maximum profit, or 0 if no profit is possible.
- Input
- One line of space-separated prices.
- Output
- Maximum profit.
Sample 1
Input
7 1 5 3 6 4
Output
5
Explanation: Buy at 1, sell at 6
Read input with input() (no prompt message) and print only the answer. Run tries the Input box; Submit checks all test cases.
Output Loading Python (first time takes a few seconds)
Press Run to see the result here.The solution unlocks after 3 submissions.