Too strict time limit with Python


  • 0
    Z

    I think my solution is one of the optimal solutions. However, it always gets TLE.

    class Solution(object):
        def lexicalOrder(self, n):
            """
            :type n: int
            :rtype: List[int]
            """
            answer = []
            for i in range(1, 10):
                if i <= n:
                    self.helper(i, answer, n)
            return answer
            
        def helper(self, num, answer, n):
            answer.append(num)
            for i in range(0, 10):
                if num*10+i <= n:
                    self.helper(num*10+i, answer, n)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.