TIme Complexity: O(N^2)
Space Complexity: O(1)
Worked for both the given inputs

def solve_knapsack(profits, weights, capacity):
max_profit = 0
for i in range(len(weights)):
for j in range(len(weights)):
if not i == j and weights[i] + weights [j] <= capacity:
max_profit = max(max_profit,profits[i] + profits[j])
return max_profit

If there is a gap in my understanding please bridge it.

Complexity is less than the recursive. Why can’t I use iterative approach?

Type your question above this line.


Hey @learn!

The beauty of Computer Science is that there are always multiple solutions to a problem. You aren’t only limited to having one single solution. There is, therefore, no harm in using the iterative approach mentioned above. I think the particular lesson simply explored a few solutions and wanted to expand only on those.