🧠
Brute Force (Pure Recursion / Nested Loops / etc.)
💡 This approach exists to build intuition by exploring all possible project selections without optimization. It helps understand the problem's combinatorial nature and why naive solutions are inefficient.
Intuition
Try every possible project that can be started with current capital, recursively explore the outcomes of picking each project, and keep track of the maximum capital achievable after K projects.
Algorithm
- If no projects can be done or K is 0, return current capital.
- For each project that can be started with current capital and not yet done:
- Recursively compute the maximum capital after picking that project and reducing K by 1.
- Return the maximum capital found among all choices.
💡 The recursion explores all combinations, which is conceptually simple but leads to exponential time due to repeated work and many branches.
def findMaxCapital(K, W, Profits, Capital):
n = len(Profits)
visited = [False] * n
def dfs(k, w):
if k == 0:
return w
max_capital = w
for i in range(n):
if not visited[i] and Capital[i] <= w:
visited[i] = True
current = dfs(k - 1, w + Profits[i])
max_capital = max(max_capital, current)
visited[i] = False
return max_capital
return dfs(K, W)
# Driver code
if __name__ == '__main__':
K = 2
W = 0
Profits = [1, 2, 3]
Capital = [0, 1, 1]
print(findMaxCapital(K, W, Profits, Capital)) # Output: 4
Line Notes
def findMaxCapital(K, W, Profits, Capital):Defines main function with inputs K, W, Profits, Capital to solve problem
visited = [False] * nTracks which projects have been chosen to avoid repeating the same project
def dfs(k, w):Recursive helper function exploring all project choices with k picks left and current capital w
if k == 0:Base case: no projects left to pick, return current capital as result
for i in range(n):Iterate over all projects to find those affordable and not yet chosen
if not visited[i] and Capital[i] <= w:Only consider projects that can be started with current capital and not already done
visited[i] = TrueMark project as chosen before recursive call to avoid reuse
current = dfs(k - 1, w + Profits[i])Recursively compute max capital after picking project i and reducing picks
max_capital = max(max_capital, current)Update max capital found among all recursive branches
visited[i] = FalseBacktrack to allow other project choices in different branches
return max_capitalReturn the best capital achievable from this state
import java.util.*;
public class IPOBruteForce {
public static int findMaxCapital(int K, int W, int[] Profits, int[] Capital) {
int n = Profits.length;
boolean[] visited = new boolean[n];
return dfs(K, W, Profits, Capital, visited);
}
private static int dfs(int k, int w, int[] Profits, int[] Capital, boolean[] visited) {
if (k == 0) return w;
int maxCapital = w;
for (int i = 0; i < Profits.length; i++) {
if (!visited[i] && Capital[i] <= w) {
visited[i] = true;
int current = dfs(k - 1, w + Profits[i], Profits, Capital, visited);
maxCapital = Math.max(maxCapital, current);
visited[i] = false;
}
}
return maxCapital;
}
public static void main(String[] args) {
int K = 2;
int W = 0;
int[] Profits = {1, 2, 3};
int[] Capital = {0, 1, 1};
System.out.println(findMaxCapital(K, W, Profits, Capital)); // Output: 4
}
}
Line Notes
public static int findMaxCapital(int K, int W, int[] Profits, int[] Capital)Main function signature to solve problem with given inputs
boolean[] visited = new boolean[n];Boolean array to track chosen projects and avoid repeats
private static int dfs(int k, int w, int[] Profits, int[] Capital, boolean[] visited)Recursive helper exploring all project choices with k picks left and capital w
if (k == 0) return w;Base case: no picks left, return current capital
for (int i = 0; i < Profits.length; i++)Try all projects to find affordable and unchosen ones
if (!visited[i] && Capital[i] <= w)Only pick projects affordable with current capital and not chosen
visited[i] = true;Mark project chosen before recursion
int current = dfs(k - 1, w + Profits[i], Profits, Capital, visited);Recurse with updated capital and picks
maxCapital = Math.max(maxCapital, current);Update max capital found
visited[i] = false;Backtrack to explore other choices
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int dfs(int k, int w, const vector<int>& Profits, const vector<int>& Capital, vector<bool>& visited) {
if (k == 0) return w;
int maxCapital = w;
for (int i = 0; i < Profits.size(); i++) {
if (!visited[i] && Capital[i] <= w) {
visited[i] = true;
int current = dfs(k - 1, w + Profits[i], Profits, Capital, visited);
maxCapital = max(maxCapital, current);
visited[i] = false;
}
}
return maxCapital;
}
int findMaxCapital(int K, int W, vector<int>& Profits, vector<int>& Capital) {
vector<bool> visited(Profits.size(), false);
return dfs(K, W, Profits, Capital, visited);
}
int main() {
int K = 2;
int W = 0;
vector<int> Profits = {1, 2, 3};
vector<int> Capital = {0, 1, 1};
cout << findMaxCapital(K, W, Profits, Capital) << endl; // Output: 4
return 0;
}
Line Notes
int dfs(int k, int w, const vector<int>& Profits, const vector<int>& Capital, vector<bool>& visited)Recursive helper exploring all project choices with k picks left and capital w
if (k == 0) return w;Base case: no picks left, return current capital
for (int i = 0; i < Profits.size(); i++)Try all projects to find affordable and unchosen ones
if (!visited[i] && Capital[i] <= w)Only pick projects affordable with current capital and not chosen
visited[i] = true;Mark project chosen before recursion
int current = dfs(k - 1, w + Profits[i], Profits, Capital, visited);Recurse with updated capital and picks
maxCapital = max(maxCapital, current);Update max capital found
visited[i] = false;Backtrack to explore other choices
vector<bool> visited(Profits.size(), false);Initialize visited array to track chosen projects
function findMaxCapital(K, W, Profits, Capital) {
const n = Profits.length;
const visited = new Array(n).fill(false);
function dfs(k, w) {
if (k === 0) return w;
let maxCapital = w;
for (let i = 0; i < n; i++) {
if (!visited[i] && Capital[i] <= w) {
visited[i] = true;
const current = dfs(k - 1, w + Profits[i]);
maxCapital = Math.max(maxCapital, current);
visited[i] = false;
}
}
return maxCapital;
}
return dfs(K, W);
}
// Test
console.log(findMaxCapital(2, 0, [1, 2, 3], [0, 1, 1])); // Output: 4
Line Notes
function findMaxCapital(K, W, Profits, Capital)Main function with inputs to solve problem
const visited = new Array(n).fill(false);Track chosen projects to avoid repeats
function dfs(k, w)Recursive helper exploring all project choices with k picks left and capital w
if (k === 0) return w;Base case: no picks left, return current capital
for (let i = 0; i < n; i++)Try all projects to find affordable and unchosen ones
if (!visited[i] && Capital[i] <= w)Only pick projects affordable with current capital and not chosen
visited[i] = true;Mark project chosen before recursion
const current = dfs(k - 1, w + Profits[i]);Recurse with updated capital and picks
maxCapital = Math.max(maxCapital, current);Update max capital found
visited[i] = false;Backtrack to explore other choices
At each of K steps, we try up to n projects, leading to exponential branching. The visited array uses O(n) space.
💡 For n=10 and K=3, this means up to 10^3=1000 recursive calls, which grows quickly and becomes infeasible for large inputs.
Interview Verdict: TLE
This approach is too slow for large inputs but is useful to understand the problem and motivate better solutions.