Intuition & Algorithm
Numbers 1-9 can be seen as candidates in the previous questions, the constraints of this question are:
- We can only pick one number one time, but we can only pick
k
numbers. - All numbers are unique
Actually, this question is much easier than before since numbers are unique and we can only pick one number one time. If we pick these numbers by ascending order, there will be no duplicates. We only need to record the depth of the path in dfs, ensuring it no more than k
.
Code
1 | class Solution { |
Complexity
Time Complexity: $O(9^k)$
Space Complexity: $O(km)$ where m is the number of solutions, it’s a math problem and I’m not sure what it is.