Short Python Solution using DP and Combinatorics


  • 0
    U
    class Solution(object):
        def countNumbersWithUniqueDigits(self, n):
            """
            :type n: int
            :rtype: int
            """
            n = min(n, 9)
            last = 0
            cache = 0
            prod = 1
            
            for index in range(1, n):
                cache = index * prod
                cache += last
                last = cache
                prod *= (10 - index)
            
            return (10 * prod + cache) if n > 0 else 1
                
            
    

Log in to reply
 

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