BuySellStock2.md
May 25, 2026 ยท View on GitHub
Problem statement: Given an integer array prices where prices[i] is the price of a stock on the i-th day, find the maximum profit you can achieve. You may buy and sell the stock multiple times, but you may not hold more than one share at a time.
Examples:
Input: prices = [7,1,5,3,6,4] Output: 7
Input: prices = [1,2,3,4,5] Output: 4
Input: prices = [7,6,4,3,1] Output: 0
Algorithmic Steps (Greedy โ accumulate positive daily gains):
- Initialize
maxProfit = 0. - Iterate from index
1to end. - If
prices[i] - prices[i-1] > 0, add that difference tomaxProfit. - Return
maxProfit.
Algorithmic Steps (Peak-Valley):
- Initialize
maxProfit = 0,i = 0. - While
i < prices.length - 1:- Advance
iwhileprices[i] >= prices[i+1]to find the next valley. - Advance
iwhileprices[i] <= prices[i+1]to find the next peak. - Add
peak - valleytomaxProfit.
- Advance
- Return
maxProfit.
Time and Space complexity:
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Greedy (positive daily gains) | O(n) | O(1) |
| Peak-Valley | O(n) | O(1) |