🧠
Dynamic Programming with State Array
💡 This approach explicitly uses a DP array to store max profit up to each day, making the state and recurrence clear for beginners.
Intuition
At each day, the max profit is either the same as previous day or selling today after buying at the lowest price so far.
Algorithm
- Initialize dp array of length n with zeros.
- Initialize min_price to prices[0].
- Iterate from day 1 to n-1:
- Update min_price if current price is lower.
- Calculate profit by selling today: prices[i] - min_price.
- Set dp[i] as max of dp[i-1] and profit.
- Return dp[n-1] as the answer.
💡 This approach makes the recurrence explicit and easy to trace.
Recurrence:dp[i] = max(dp[i-1], prices[i] - min_price_so_far)
def maxProfit(prices):
n = len(prices)
dp = [0] * n
min_price = prices[0]
for i in range(1, n):
if prices[i] < min_price:
min_price = prices[i]
dp[i] = max(dp[i-1], prices[i] - min_price)
return dp[-1]
# Driver code
if __name__ == '__main__':
prices = [7,1,5,3,6,4]
print(maxProfit(prices)) # Output: 5
Line Notes
dp = [0] * nInitialize dp array to store max profit up to each day
min_price = prices[0]Start min_price as first day's price
for i in range(1, n)Iterate from second day to last day
if prices[i] < min_priceUpdate min_price if current price is lower
dp[i] = max(dp[i-1], prices[i] - min_price)Store max profit up to day i
public class Solution {
public static int maxProfit(int[] prices) {
int n = prices.length;
int[] dp = new int[n];
int minPrice = prices[0];
for (int i = 1; i < n; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
dp[i] = Math.max(dp[i-1], prices[i] - minPrice);
}
return dp[n-1];
}
public static void main(String[] args) {
int[] prices = {7,1,5,3,6,4};
System.out.println(maxProfit(prices)); // Output: 5
}
}
Line Notes
int[] dp = new int[n];Create dp array to store max profit up to each day
int minPrice = prices[0];Initialize minPrice to first day's price
for (int i = 1; i < n; i++)Iterate from second day to last day
if (prices[i] < minPrice)Update minPrice if current price is lower
dp[i] = Math.max(dp[i-1], prices[i] - minPrice);Store max profit up to day i
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxProfit(vector<int>& prices) {
int n = prices.size();
vector<int> dp(n, 0);
int minPrice = prices[0];
for (int i = 1; i < n; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
dp[i] = max(dp[i-1], prices[i] - minPrice);
}
return dp[n-1];
}
int main() {
vector<int> prices = {7,1,5,3,6,4};
cout << maxProfit(prices) << endl; // Output: 5
return 0;
}
Line Notes
vector<int> dp(n, 0);Initialize dp vector with zeros for max profit
int minPrice = prices[0];Set minPrice to first day's price
for (int i = 1; i < n; i++)Iterate from second day to last day
if (prices[i] < minPrice)Update minPrice if current price is lower
dp[i] = max(dp[i-1], prices[i] - minPrice);Store max profit up to day i
function maxProfit(prices) {
const n = prices.length;
const dp = new Array(n).fill(0);
let minPrice = prices[0];
for (let i = 1; i < n; i++) {
if (prices[i] < minPrice) {
minPrice = prices[i];
}
dp[i] = Math.max(dp[i-1], prices[i] - minPrice);
}
return dp[n-1];
}
// Test
console.log(maxProfit([7,1,5,3,6,4])); // Output: 5
Line Notes
const dp = new Array(n).fill(0);Initialize dp array with zeros for max profit
let minPrice = prices[0];Initialize minPrice to first day's price
for (let i = 1; i < n; i++)Iterate from second day to last day
if (prices[i] < minPrice)Update minPrice if current price is lower
dp[i] = Math.max(dp[i-1], prices[i] - minPrice);Store max profit up to day i
Single pass with dp array of size n.
💡 Uses more memory than approach 2 but makes DP states explicit.
Interview Verdict: Accepted; useful for understanding DP formulation.
Good for teaching DP but less optimal in space.