Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Best Sum

Given a target number target and a list of positive integers numbers, the goal is to find the shortest combination of elements in numbers that adds up to the target. If it is not possible to reach the target, the function should return None.

Examples

Input: target=7, numbers=[5, 3, 4, 7]
Output: [7]

Explanation: The number 7 is the only element in numbers that adds up to 7.
Input: target=8, numbers=[2, 3, 5]
Output: [3, 5]

Explanation: The numbers 3 and 5 add up to 8.

Approach

Three different functions have been implemented to solve this problem:

  • find_shortest_combination_sum_basic: This function uses a recursive approach to find the shortest combination of elements that adds up to the target. It starts by checking if the target is negative (in which case it returns float('inf')) or if the target is 0 (in which case it returns 0). If neither of those conditions is met, it iterates over numbers and finds the minimum combination length by adding 1 to the recursive call to itself using the target minus the current element. Finally, it reconstructs the combination by appending the elements that were used in the recursive calls to reach the target.

  • find_shortest_combination_sum_memo: This function is similar to find_shortest_combination_sum_basic, but it includes a memoization step to store the results of recursive calls and avoid calculating them multiple times. This improves the performance of the function by reducing the number of recursive calls.

  • find_shortest_combination_sum_table: This function uses a dynamic programming approach to solve the problem. It creates a table of lists with length $target + 1$, initialized to None, and sets the first element to an empty list. Then, it iterates over the table and, for each element that is not None, it appends the current element to the list at index $i + number$ (where i is the current index and number is an element in numbers) if $i + number$ is less than or equal to the target and the list at that index is None or has a greater length than the current list. Finally, it returns the value of the last element in the table, which will be the shortest combination of elements that adds up to the target or None if it is not possible to reach the target.

Complexity

The complexity of the different solutions is as follows:

  • find_shortest_combination_sum_basic: This function has a time complexity of $O(2^n)$, where n is the length of numbers, because it makes two recursive calls for each element in numbers. It has a space complexity of $O(n)$, because the call stack grows by n at each recursive call.

  • find_shortest_combination_sum_memo: This function has a time complexity of $O(n * target)$, where n is the length of numbers, because it iterates over numbers once for each element of the target. It has a space complexity of $O(n * target)$, because it stores the results of the recursive calls in a dictionary with $n * target elements$.

  • find_shortest_combination_sum_table: This function has a time complexity of $O(n * target)$, where n is the length of numbers, because it iterates over the elements in numbers once for each element of the target. It has a space complexity of $O(target)$, because it creates a table with target elements. The space complexity is $O(n^2)$, since the table used to store the results of subproblems has a size of $n^2$.