0 ms Java Solution using DP and combinations


  • 0
    V
    public int countNumbersWithUniqueDigits(int n) {
        /*base cases */
        if(n <= 0) return 1; 
        if(n == 1 ) return 10;
        if(n == 2) return 91;
        n = Math.min(10,n); // number greater than 10 digit will not satisfy required condition.
        /* result = previous digit result + current digit
        fact is used to use previous multiplication */
        int fact = 81, dp = 91; 
        for(int i = 3 ; i <= n ; i++){
            fact *= (11 - i);
            dp += fact  ;
        }
        return dp;
    }

Log in to reply
 

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