Link:
https://leetcode.com/problems/count-numbers-with-unique-digits/
My Code:
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
import math as m
ResultList = [1,10]
for j in range(2,n+1):
if j<=10:
Result = ResultList[j-1]+int(9*m.factorial(9)/(m.factorial(10-j)))
ResultList.append(Result)
else:
return ResultList[10]
return ResultList[n]
Explanation: The idea is clear. First, there are only 10 different digits, so for any n that is larger than 10, we shall consider it exactly as the situation with n = 10. Next, for each n <=10, we construct a dp, dp[n] is the total number of numbers with unique digits with maximum n digits. Then for n+1, we just need to calculate the number of numbers with unique digits at length equals to n+1, then sum it with dp[n] to get the total number of numbers with unique digits at length at most n+1. Then return it.