Backend Engineer
Senior
programming
You are given a list of stock prices for a given day where prices[i] is the price of a given stock on the ith day. Find the maximum profit you can achieve if you buy on one day and sell on another later day. You must solve it in O(n) time complexity. ### Constraints: - 1 <= prices.length <= 10^5 - 0 <= prices[i] <= 10^4 ### Examples: 1. Input: prices = [7, 1, 5, 3, 6, 4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6 - 1 = 5. 2. Input: prices = [7, 6, 4, 3, 1] Output: 0 Explanation: In this case, no transactions are done and the profit is 0.
Suggested Answer