Understanding Lexicographical Order
Lexicographical order is similar to the order used in dictionaries. When sorting numbers lexicographically, you treat them as strings and compare them character by character from left to right.
Example:
Consider the numbers from 1 to 13:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
When sorted lexicographically, they are ordered as:
1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9
Why?
'10' comes before '2' because '1' (the first character of '10') is less than '2'.
- Similarly,
'11', '12', and '13' follow '10' because they share the same prefix '1'.
Approach to Solve the Problem
To generate all numbers from 1 to n in lexicographical order efficiently (i.e., in O(n) time and O(1) extra space), we can use a DFS-like (Depth-First Search) approach to traverse the numbers as if they form a tree.
Tree Representation:
- Each number represents a node.
- Each node has up to 10 children, corresponding to appending digits
0 to 9 to the current number.
Steps:
- Start with
1.
- Go as deep as possible by appending
0 to 9 to the current number.
- If appending a digit exceeds
n, backtrack and try the next digit.
- Repeat until all numbers are traversed.
This ensures that we visit numbers in lexicographical order without the need for additional space for sorting or recursion stack.
Implementation
Here's how you can implement this approach in Python:
class Solution:
def lexicalOrder(self, n: int) -> List[int]:
result = []
current = 1
for _ in range(n):
result.append(current)
# Try to go deeper by multiplying by 10
if current * 10 <= n:
current *= 10
else:
# If we can't go deeper, increment
if current >= n:
current //= 10
current += 1
# If current is a multiple of 10, keep dividing by 10 until it's not
while current % 10 == 0:
current //= 10
return result
Explanation of the Code
-
Initialize:
result list to store the lexicographical order.
- Start with
current = 1.
-
Loop n times:
- Append
current to the result.
-
Attempt to Go Deeper:
- Multiply
current by 10 to go to the next level (e.g., 1 → 10).
- Check if
current * 10 <= n to ensure we don't exceed the limit.
-
If Cannot Go Deeper:
- Increment
current by 1 (e.g., 13 → 2).
- If
current exceeds n, backtrack by dividing by 10.
- Remove trailing zeros: If
current ends with a 0, keep dividing by 10 to remove them.
-
Repeat: Continue this process until all numbers are added to result.
Example Trace for n = 13
current = 1
result = [1]
current = 10 (1 * 10)
result = [1, 10]
current = 11 (10 + 1)
result = [1, 10, 11]
current = 12
result = [1, 10, 11, 12]
current = 13
result = [1, 10, 11, 12, 13]
current = 2 (backtrack and increment)
result = [1, 10, 11, 12, 13, 2]
current = 3
result = [1, 10, 11, 12, 13, 2, 3]
current = 4
result = [1, 10, 11, 12, 13, 2, 3, 4]
current = 5
result = [1, 10, 11, 12, 13, 2, 3, 4, 5]
current = 6
result = [1, 10, 11, 12, 13, 2, 3, 4, 5, 6]
current = 7
result = [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7]
current = 8
result = [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8]
current = 9
result = [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
Time and Space Complexity
Conclusion
This approach efficiently generates the lexicographical order of numbers from 1 to n by simulating a depth-first traversal of a number tree. It ensures optimal time and space usage by avoiding unnecessary sorting and recursion.