Skip to content
BytePatterns

Digit Orderings Under a Limit

EasyBacktracking#backtracking#permutations#pruning~15m

Problem

Given a list of distinct digits and a limit, return every number that uses each digit exactly once, in some order, and is at most the limit, in increasing order. A number may not start with 0 unless it is the single digit 0. There are at most 8 digits.

Examples

Input:  digits = [3, 1, 2], limit = 300
Output: [123, 132, 213, 231]
Why:    312 and 321 are over the limit, and trying digits in increasing order yields the numbers already sorted
Input:  digits = [0, 2, 1], limit = 999
Output: [102, 120, 201, 210]
Why:    orderings that start with 0 are skipped, because 012 is really a two-digit number
Input:  digits = [5], limit = 4
Output: []
Why:    edge case, the only ordering is already over the limit

Hints

0 / 3

Stuck on the idea rather than the code? Permutations covers it.