All problems
Stock Profit with One Transaction
Problem
Given daily prices, return the maximum profit from buying one share on some day and selling it on a later day, or 0 if no profit is possible.
Track the minimum price seen so far and the best profit from selling today. The list may be empty or strictly decreasing.
Hint
The best sale today uses the cheapest price seen before today.
Test cases
case 1
in: [[7,1,5,3,6,4]]
expected: 5
case 2
in: [[7,6,4,3,1]]
expected: 0
case 3
in: [[]]
expected: 0
case 4
in: [[1]]
expected: 0
case 5
in: [[3,3,5,0,0,3,1,4]]
expected: 4
Starter codePython
def max_profit_one(prices):
# Your code here
pass5 test cases · run your code against them in the editor