A finance dashboard shows daily prices of one stock in prices. You may buy one share on one day and sell it on a later day, at most once.
Write oneTradeProfit(prices) that returns the largest profit you can make. If no trade makes money, return 0.
Input: prices = [8,2,6,1,5,4] Output: 4
Buy at 2 and sell at 6, or buy at 1 and sell at 5.
Input: prices = [9,7,4,1] Output: 0
Prices only fall, so it is best not to trade.
1 <= prices.length <= 1000000 <= prices[i] <= 10000Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #552 One Trade on a Price Chart. 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.