- Time complexity in brute force sum possible??
Statement: Given a number n
Approach: It is possible to solve this problem with brute force recursion.
def fib(n):
if n == 0:
return 0
if n == 1:
return 1
return fib(n-1) + fib(n-2)
Nevertheless this leads to repeated work. For example, the call to fib(3)
will call fib(2)
and fib(1)
, and the call to fib(2)
will call fib(1)
and fib(0)
. This means that the call to fib(1)
is repeated.
Leading to a complexity of
A better approach is to use dynamic programming and store the results of the subproblems.
Complexity:
- Time:
$O(n)$ - Space:
$O(n)$
Statement: Given a list of numbers
Approach:
Is possible to solve this problem with brute force recursion. The idea is to substract all possible combinations of nums
from a
until reaching 0.
- Code the two base cases:
a. Amount is 0, then we succesfully got to
$a$ . b.
def sum_possible(a, nums):
if a < 0:
return False
if a == 0:
return True