Best Time to Buy and Sell Stock
DP on Stocks DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Maximum profit with single buy and sell in Best Time to Buy and Sell Stock problem, Arrays, Loops, Optimization, Time Complexity, Space Complexity, Greedy Algorithm, Edge Cases, complexity analysis, greedy algorithm, algorithm design, array traversal, tracking values, Single-Pass Algorithms.
You are given an array of integers 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. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Input Specification: The input will be a single array of integers, prices. Output Specification: Return a single integer, the maximum profit. Constraints: Constraints are not explicitly provided in the original content. Sample Input 1: prices = [7,1,5,3,6,4] Sample Output 1: 5 Explanation: Buy on day 1 (price = 1) and sell on day 4 (price = 6), profit = 6 1 = 5.