Problem · Array
Best Time to Buy and Sell Stock
Learn this problemProblem statement
You are given an array prices, where prices[i] is the price of one share on day i.
Choose at most one day to buy and one later day to sell. Return the maximum profit. If no profitable transaction exists, return 0.
Function
maxProfit(prices: int[]) → longExamples
Example 1
prices = [7,1,5,3,6,4]return = 5Buy at price 1 and sell later at price 6.
Example 2
prices = [7,6,4,3,1]return = 0Every later price is lower, so the best choice is not to trade.
Constraints
1 <= prices.length <= 2000000 <= prices[i] <= 10^9
More Oracle problems
- Add One to a Number Represented as DigitsONSITE INTERVIEW · Seen Jul 2026
- Top K Frequent Elements with Larger-Value Tie BreakONSITE INTERVIEW · Seen Jul 2026
- Merge k Sorted ListsPHONE SCREEN · Seen Jul 2026
- Implement a Queue Using Two StacksPHONE SCREEN · Seen Jul 2026
- First Balanced Removal IndexOA · Seen Dec 2025
- Find Circle NumberOA · Seen Oct 2024
- Create Lexicographically Largest PermutationOA · Seen Sep 2024
- Array Reduction 1OA · Seen Feb 2024