Smallest Permutation Above a Lower Bound

Asked at AirbnbPhone screen

Reported by the problem's creator, not verified

You are given the digits of a number as a list, and a lower bound. Rearrange all of the digits, each used exactly once, into the smallest number that is at least the lower bound.

Write smallest_permutation(digits, lower_bound).

Examples

  1. Example 1

    Input: digits = [7,1,8], lower_bound = 719

    Output: 781

    Explanation: Of the rearrangements of 7, 1 and 8, 718 is below 719 and 781 is the smallest that reaches it.

  2. Example 2

    Input: digits = [1,7,8], lower_bound = 200

    Output: 718

    Explanation: 178 and 187 are below 200, and 718 is the smallest rearrangement of 1, 7 and 8 that reaches it.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.