🧠
Brute Force (Try All Buy-Sell Pairs)
💡 This approach exists to understand the problem deeply by exploring all possible transactions. It is inefficient but helps grasp the problem's combinatorial nature.
Intuition
Try every possible pair of buy and sell days and sum profits of all valid transactions to find the maximum total profit.
Algorithm
- For each day i, consider buying the stock.
- For each day j > i, consider selling the stock.
- Calculate profit if prices[j] > prices[i].
- Recursively explore all sequences of transactions to find the maximum total profit.
💡 This algorithm is hard because it tries all combinations, leading to exponential complexity and overlapping subproblems.
def maxProfit(prices):
def dfs(i):
if i >= len(prices):
return 0
max_profit = 0
for j in range(i+1, len(prices)):
if prices[j] > prices[i]:
profit = prices[j] - prices[i] + dfs(j+1)
max_profit = max(max_profit, profit)
skip = dfs(i+1)
return max(max_profit, skip)
# Driver code
if __name__ == '__main__':
prices = [7,1,5,3,6,4]
print(maxProfit(prices))
Line Notes
def dfs(i):Defines recursive helper to explore transactions starting at day i
if i >= len(prices):Base case: no more days left to trade
for j in range(i+1, len(prices))Try selling on every day after buying day i
if prices[j] > prices[i]:Only consider profitable transactions
profit = prices[j] - prices[i] + dfs(j+1)Profit from current transaction plus future profits
skip = dfs(i+1)Option to skip buying on day i
return max(max_profit, skip)Choose the best between buying or skipping
public class Solution {
public int maxProfit(int[] prices) {
return dfs(prices, 0);
}
private int dfs(int[] prices, int i) {
if (i >= prices.length) return 0;
int maxProfit = 0;
for (int j = i + 1; j < prices.length; j++) {
if (prices[j] > prices[i]) {
int profit = prices[j] - prices[i] + dfs(prices, j + 1);
maxProfit = Math.max(maxProfit, profit);
}
}
int skip = dfs(prices, i + 1);
return Math.max(maxProfit, skip);
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] prices = {7,1,5,3,6,4};
System.out.println(sol.maxProfit(prices));
}
}
Line Notes
public int maxProfit(int[] prices)Entry point for the solution
return dfs(prices, 0);Start recursion from day 0
if (i >= prices.length) return 0;Base case: no more days to trade
for (int j = i + 1; j < prices.length; j++)Try all sell days after buy day i
if (prices[j] > prices[i])Only consider profitable transactions
int profit = prices[j] - prices[i] + dfs(prices, j + 1);Profit plus future profits
int skip = dfs(prices, i + 1);Option to skip buying on day i
return Math.max(maxProfit, skip);Choose best option
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int dfs(const vector<int>& prices, int i) {
if (i >= prices.size()) return 0;
int maxProfit = 0;
for (int j = i + 1; j < prices.size(); ++j) {
if (prices[j] > prices[i]) {
int profit = prices[j] - prices[i] + dfs(prices, j + 1);
maxProfit = max(maxProfit, profit);
}
}
int skip = dfs(prices, i + 1);
return max(maxProfit, skip);
}
int maxProfit(vector<int>& prices) {
return dfs(prices, 0);
}
};
int main() {
Solution sol;
vector<int> prices = {7,1,5,3,6,4};
cout << sol.maxProfit(prices) << endl;
return 0;
}
Line Notes
int dfs(const vector<int>& prices, int i)Recursive helper exploring transactions from day i
if (i >= prices.size()) return 0;Base case: no more days to trade
for (int j = i + 1; j < prices.size(); ++j)Try all sell days after buy day i
if (prices[j] > prices[i])Only consider profitable transactions
int profit = prices[j] - prices[i] + dfs(prices, j + 1);Profit plus future profits
int skip = dfs(prices, i + 1);Option to skip buying on day i
return max(maxProfit, skip);Choose best option
function maxProfit(prices) {
function dfs(i) {
if (i >= prices.length) return 0;
let maxProfit = 0;
for (let j = i + 1; j < prices.length; j++) {
if (prices[j] > prices[i]) {
let profit = prices[j] - prices[i] + dfs(j + 1);
maxProfit = Math.max(maxProfit, profit);
}
}
let skip = dfs(i + 1);
return Math.max(maxProfit, skip);
}
return dfs(0);
}
// Test
console.log(maxProfit([7,1,5,3,6,4]));
Line Notes
function dfs(i)Recursive helper to explore transactions from day i
if (i >= prices.length) return 0;Base case: no more days to trade
for (let j = i + 1; j < prices.length; j++)Try all sell days after buy day i
if (prices[j] > prices[i])Only consider profitable transactions
let profit = prices[j] - prices[i] + dfs(j + 1);Profit plus future profits
let skip = dfs(i + 1);Option to skip buying on day i
return Math.max(maxProfit, skip);Choose best option
TimeO(2^n)
SpaceO(n) due to recursion stack
For each day, we decide to buy or skip, leading to exponential branching.
💡 For n=20, this means over a million recursive calls, which is impractical.
Interview Verdict: TLE
This approach is too slow for large inputs but helps understand the problem and motivates optimization.