Why it works
- Each call takes only numbers after the last one in the path, so every combination is built once, in increasing order, and [2, 1] never repeats [1, 2].
- A path is saved exactly when it holds k numbers; then removing the last number lets the loop put the next one in its place.
- With need places left, a start above n − need + 1 leaves fewer than need numbers, so that branch cannot finish: the loop bound skips it without going down.
Complexity
Time O(k × C(n, k)): the bound lets every path that is started reach k numbers, so the calls are at most k per answer, and each answer is a copy of k numbers. Memory O(k) besides the answer: the path and the call stack.
nextNow solve it yourself, no hints.
Solve on LeetCode ↗