I do not believe that you can improve on O(N), where N is the number of digits in the number. However, I believe you can improve on the computation cost, since the term "minimum" implies left-to-right ordering.
Consider a number represented by ABC... where the "..." represents an arbitrary number of additional digits.
If A>B, then all possible solutions must begin with A.
This can be seen in your example, where all possible solutions begin with 8.
If A<=B, then possible solutions can begin with both A and B, but since A is lower, only those that start with A can possibly meet the criteria of minimum.
So, we know that the first digit will always represent the first digit in the answer!
Taking this a step further, if A<=B, the minimum solution will always be the solution to BC... prefixed with A. Consider 7829, where the minimum solution 789 is just 7 prefixed to the solution of 829.
But, if A>B, then B is optional in the minimum solution, allowing the choice - if B>C, then the minimum solution must be AC... by replacing AB=>A, since AB... would always be larger than AC..., short-cutting the solution as soon as this condition is met on any sub-solution.
Note that this is still O(N) due to the worst-case scenario for entirely ascending digits, but may eliminate some of the individual steps, which I believe is the best that can be done with this.
889
, is the generated set[88, 89]
or is it just[89]
? – LinuxDisciple