377 Combination Sum IV
377. Combination Sum IV
1. Question
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
Follow up: What if negative numbers are allowed in the given array? How does it change the problem? What limitation we need to add to the question to allow negative numbers?
2. Implementation
(1) DP
思路: 每个nums的元素都可以不停地被用,很明显的完全背包类问题,按照模板写就好了。至于follow-up,如果允许负数的话,我们无法确定最后有多少种combination得到target. 如果要加条件使得我们在有负数的情况下得到target的话,我觉得应该是每个数只能用一次。
3. Time & Space Complexity
时间复杂度O(n * target), 空间复杂度O(target)
Last updated