Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order.
You must write an algorithm that runs in O(n) time and uses O(1) extra space.
Example 1:
Input: n = 13 Output: [1,10,11,12,13,2,3,4,5,6,7,8,9]
Example 2:
Input: n = 2 Output: [1,2]
Constraints:
1 <= n <= 5 * 104When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To list numbers in lexicographical order, think of it like organizing words in a dictionary. The brute force method is to simply create all possible numbers within the given range and then sort them based on their dictionary order.
Here's how the algorithm would work step-by-step:
def lexicographical_numbers_brute_force(maximum_number):
# Create a list of numbers from 1 to maximum_number
numbers_list = list(range(1, maximum_number + 1))
# Sort the list lexicographically
numbers_list.sort(key=str)
# Ensure the result is returned
return numbers_listThe goal is to create a list of numbers in lexicographical order (like a dictionary) up to a certain limit. The efficient way is to build the numbers one digit at a time, prioritizing depth-first exploration to follow the lexicographical order.
Here's how the algorithm would work step-by-step:
def lexicographical_numbers(limit): result = []
current_number = 1
while current_number <= limit:
result.append(current_number)
# Dive deeper by multiplying by 10 if possible
if current_number * 10 <= limit:
current_number *= 10
else:
# Go back and increment if can't dive deeper
if current_number >= limit:
break
current_number += 1
# Keep incrementing until the last digit is not zero
while current_number % 10 == 0:
current_number //= 10
# Prevents going over limit
if current_number > limit:
break
return result| Case | How to Handle |
|---|---|
| n is 0 | Return an empty list because the problem asks for numbers from 1 to n. |
| n is a single digit number (1-9) | The solution should correctly generate the list [1, 2, ..., n]. |
| n is a power of 10 (e.g., 10, 100, 1000) | Ensure the solution handles the transition from 9 to 10, 99 to 100, etc., correctly in lexicographical order. |
| n is a large number (close to Integer.MAX_VALUE in Java) | The solution should be efficient enough to avoid timeouts; iterative deepening is suggested. |
| The lexicographical order exceeds the maximum integer value | The code must terminate traversal when the generated number is greater than n. |
| n contains leading zeros (should be handled as a valid integer) | The problem statement implies input is a valid integer, so leading zeros are not applicable. |
| All numbers from 1 to n have the same starting digit. | The recursion or iteration explores all subtrees fully, ensuring correct lexicographical order despite the common prefix. |
| n is a number like 111111, and many numbers have a long shared prefix. | The solution needs to avoid excessive recursion depth or unnecessary computations due to the long shared prefix. |