DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
easy
Array

Best Time to Buy and Sell Stock

**Problem Statement:** You are given an array `prices` where `prices[i]` is the price of a given stock on the `i`th day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. **Hint / Expected Approach:** Single-pass min tracking + max profit **Edge Cases to Consider:** - (1) Strictly decreasing prices (no profit) - (2) All same prices - (3) Only two elements

Examples

Example 1
Input: [7, 1, 5, 3, 6, 4]
Output: 5
Example 2
Input: [7, 6, 4, 3, 1]
Output: 0
Example 3
Input: [1, 2]
Output: 1

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪Strictly decreasing prices (no profit)
  • ▪All same prices
  • ▪Only two elements
Frequently Asked At
AmazonMicrosoftMetaAppleUberZomato