A trading widget shows a stock's closing price for each day in prices. You may buy once and sell once, on a later day.
Write bestTrade(prices) that returns the largest profit you can make. If no trade makes money, return 0.
Input: prices = [9,4,6,2,8,5] Output: 6
Buy at 2 on day 3 and sell at 8 on day 4.
Input: prices = [9,7,4,1] Output: 0
The price only falls, so don't trade.
1 <= prices.length <= 2 * 10^50 <= prices[i] <= 10^4Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #61 Best Single Trade Window. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.