Search⌘ K
AI Features

Solution: Combinations

Explore how to solve the combinations problem using backtracking. This lesson guides you through building all size-k combinations from numbers 1 to n, implementing recursive calls, pruning paths, and managing state with a temporary list. You will understand the algorithm’s logic, coding steps, and 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 ...