Skip to content
RED SAGSkills
easy15 ptsarrays

Best Single Trade Profit

prices[i] is the price of a token on day i. You may buy once and sell once on a later day. Return the largest profit you can make, or 0 if no profitable trade exists.

Example 1

Input: prices = [9, 2, 6, 1, 7, 4]
Output: 6
Explanation: Buy at 1 on day 3, sell at 7 on day 4.

Example 2

Input: prices = [8, 6, 3]
Output: 0

Constraints

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4
Hint 1

Track the cheapest price seen so far.

Hint 2

At each day, the best sale is today minus that cheapest price.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer, Data Scientist, Blockchain Developer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.