Best Sum
Brute force
def best_sum_brute(target_sum: int, numbers: list[int]):
"""
Time complexity: O(N^M * M)
Time complexity will depend on two factors:
- how big is value of target_sum (M)
- and the number of items in numbers (N).
In the algorigthm, we explore all the available solutions
by iterating the available "numbers" to decrease
the value of the target.
Then, the branching factor will be N and,
in the worst scenario, wewill explore these M options
up to M times.
Additionally, for every iteration we copy all the
elements of the solution and we add an element.
This list of elements will be bounded by M given
its lenght could be M in the worst case.
Space complexity: O(M*M) = O(M²)
The height of the recursion tree will depend on
target value. Thus, it will depend on M.
Also, we are keeping every potencial best
solution in every recursion level, with a max length of M.
"""
if target_sum == 0:
return []
if target_sum < 0:
return None
best_solution = None
for number in numbers:
diff = target_sum - number
result = best_sum_brute(diff, numbers)
if result is not None:
solution = [*result, number]
if (
best_solution is None
or len(solution) < len(best_solution)
):
best_solution = solution
return best_solutionMemoization
Tabulation
Last updated
Was this helpful?