Essentially the same as the How Sum question but our goal here is to find the shortest array that yields the sum we are interested in. If there are multiple equally short options then we can take any one
Base Implementation
The solution to this is almost identical to the How Sum questio but we don’t early return when we find a solution of a subtree but instead we store the result of the best subtree as we iterate through
In the above solution with m=target and n=lengthofnums the time complexity is O(nm∗m) and the space complexity is O(m2)
With memoization
We can implement memoizatio based on the target:
The time complexity of this should now be O(m2∗n) and the space complexity of O(m2)