Show problems as
Classic problem #77MediumBacktracking

Combinations

Real-world version: Tasting Flight Line-ups
Tracked in this browser only
Read Combinations on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A cellar stores casks barrels, numbered 1 through casks. A tasting flight is a selection of exactly pour different barrels; two flights are the same flight when they draw from the same set of barrels, no matter which barrel was poured first.

Return every flight the cellar can serve. Each flight must list its barrel numbers in increasing order, and the flights themselves may be returned in any order.

Examples

Example 1

Input
casks = 5, pour = 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],[3,4,5],[2,4,5]]

Ten different sets of three barrels can be drawn from five, and each one is listed with its barrel numbers increasing.

Example 2

Input
casks = 3, pour = 3
Output
[[1,2,3]]

A flight of three from three barrels can only draw all of them, so there is a single flight.

Example 3

Input
casks = 2, pour = 1
Output
[[2],[1]]

Each barrel on its own is a flight of one, so both single-barrel flights are returned; the order of the flights does not matter.

Constraints

  • 1 <= casks <= 20
  • 1 <= pour <= casks

The values you return may be in any order.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def list_flights(casks: int, pour: int) -> list[list[int]]:
Java
public List<List<Integer>> listFlights(int casks, int pour)