Search⌘ K
AI Features

Solution: Combinations

Explore how to generate all unique combinations of k numbers chosen from a range of 1 to n using the backtracking pattern. This lesson guides you through the recursive algorithm, showing how to systematically build combinations and prune unnecessary paths. Gain the ability to implement this approach efficiently and understand its time and space complexity.

Statement

You are given two integers, n and k. Your task is to return all possible combinations of k numbers chosen from the range [1, n].

The result can be returned in any order.

Note: Combinations are unordered, i.e., [1, 2] and [2, 1] are considered the same combination.

Constraints:

  • 11 \leq n 20\leq 20 ...