Dsa
Dynamic Programming
Medium
Best Time to Buy and Sell Stock II
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. You may make as many transactions as you like, but you must sell the stock before you buy again. Print the maximum profit you can achieve.
Input Format:
- A single line containing space-separated integers representing the prices on each day (1 <= n <= 10000).
Output Format:
- A single integer representing the maximum profit.
Example:
Input:
7 1 5 3 6 4
Output:
5
Key concepts
dynamic_programminggreedarray
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now