Tap here to switch tabs
Problem
Submissions
Solution

Solution: Combination Sum

Statement

Naive approach

A naive approach to solve this problem would be to make all the possible combinations of nums or generate all subsets of nums that sum up to the required target.

This solution is very costly because the time complexity of achieving this is O(nt/m)O(n^{t/m}), where nn is the number of elements in nums, tt is the target, and mm is the minimum value in nums. This is because each node in the recursion tree can have nn branches, and the height of the tree can grow up to t/mt/m. Therefore, the total number of nodes in such a tree is bounded by nt/mn^{t/m}.

Optimized approach using dynamic programming

Because the recursive solution to this problem is very costly, let’s see if we can reduce this cost in any way. Dynamic programming helps us avoid recomputing the same subproblems. Now let’s analyze our recursive solution to see if it has the properties needed for conversion to dynamic programming.

  • Optimal substructure: Let’s say we want to find the solution to the problem of counting a value, target, given nn numbers. Since we know the answer to the n ...

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Combination Sum

Statement

Naive approach

A naive approach to solve this problem would be to make all the possible combinations of nums or generate all subsets of nums that sum up to the required target.

This solution is very costly because the time complexity of achieving this is O(nt/m)O(n^{t/m}), where nn is the number of elements in nums, tt is the target, and mm is the minimum value in nums. This is because each node in the recursion tree can have nn branches, and the height of the tree can grow up to t/mt/m. Therefore, the total number of nodes in such a tree is bounded by nt/mn^{t/m}.

Optimized approach using dynamic programming

Because the recursive solution to this problem is very costly, let’s see if we can reduce this cost in any way. Dynamic programming helps us avoid recomputing the same subproblems. Now let’s analyze our recursive solution to see if it has the properties needed for conversion to dynamic programming.

  • Optimal substructure: Let’s say we want to find the solution to the problem of counting a value, target, given nn numbers. Since we know the answer to the n ...