/77. Combinations

77. Combinations

Medium
Backtracking74.3% acceptance

Given two integers upper_bound and selection_count, return all possible unordered combinations of selection_count distinct integers chosen from the range [1, upper_bound]. Each combination must contain selection_count unique integers, and the order of integers within a combination does not matter. The output can be in any order.

Example 1

Input: upper_bound=5, selection_count=3

Output: [[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]]

Explanation: There are 10 possible combinations of 3 numbers from 1 to 5.

Example 2

Input: upper_bound=3, selection_count=2

Output: [[1,2],[1,3],[2,3]]

Explanation: There are 3 possible combinations of 2 numbers from 1 to 3.

Constraints

  • 1 <= upper_bound <= 20
  • 1 <= selection_count <= upper_bound
Python (current runtime)

Case 1

Input: upper_bound=6, selection_count=2

Expected: [[1,2],[1,3],[1,4],[1,5],[1,6],[2,3],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]

Case 2

Input: upper_bound=2, selection_count=2

Expected: [[1,2]]

Case 3

Input: upper_bound=7, selection_count=1

Expected: [[1],[2],[3],[4],[5],[6],[7]]