Problem statement

https://binarysearch.com/problems/N-Lexicographic-Integers/

Solution

Equal to Leetcode 0386 Lexicographical Numbers.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, n):
        def dfs(num):     
            self.result.append(num)
            for i in range(1 if num == 0 else 0, 10):
                if num*10 + i > n: break
                dfs(num*10 + i)
                
        self.result = []
        dfs(0)
        return self.result[1:]