Trains the technique from
LeetCode 386Lexicographical NumbersThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A stationery warehouse tags its picking bins with the whole numbers 1 through n. A tag is printed with no padding of any kind, so bin seven carries the tag 7 and bin seventy carries the tag 70.
The stock terminal sorts tags the way it sorts words. It lines two tags up at their first character and walks rightwards, and the first character where they differ decides the order, the earlier character winning. If one tag runs out while matching the other character for character all the way, the shorter tag is placed first.
Return the bin numbers in the order the terminal lists their tags.
Produce the listing by stepping from one tag to the next rather than by sorting: spend time proportional to n and keep only a constant amount of working memory aside from the listing you return.
Example 1
Every tag is a single character here, so walking the characters puts them in the same order as counting.
Example 2
The tags 10 and 11 both open with the character 1 and continue past where the tag 1 ends, so they follow it and precede the tag 2.
Example 3
All six tags opening with the character 1 sit together in order of their second character, and the single-character tags 2 through 9 come afterwards.
Example 4
The block opening with the character 1 now runs to the tag 19, and the remaining single-character tags follow it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def lexical_order(n: int) -> list[int]:public List<Integer> lexicalOrder(int n)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.