Java 0ms solution with explanations
The idea is simple. Count all numbers that do not have repeating digits and less than or equal to N. Substract the count from N and return. My initial idea was 
The idea is simple. Count all numbers that do not have repeating digits and less than or equal to `N`. Substract the count from `N` and return. My initial idea was to permutate 10 digits  in [1..`digsCount`] slots (`digsCount` is a number of digits in `N`) and take into account those results of permutation that are <= `N` only. And this worked, but the submition time was  ~1400ms.\n\nAn alternative approach uses similar idea but instead of generating permutations we can count them using \'number of permutations of n objects taken r at a time\' formula *P(n,r)=n!/(n\u2212r)!*. \nBecause digit 0 cannot be a highest digit (number \'010\' does not make sence, there is number \'10\' instead), we need to adjust the formula *P\'(n,r) = (n-1) * P(n - 1,r - 1)=(n-1)(n-1)!/(n\u2212r)!* In this case *n*  equals to 10, *r* is in [1..`digsCount`). *P\'(r)=9  9! / (10 \u2212 r)!*\n\nLet\'s consider number 112.\n`digsCount` = `getDigits(N, digits)` = 3\n`digits` = [2, 1, 1]\n`uniq` = `initPermCount(digsCount)` = *P\'(1) + P\'(2)* = 9 * 9! / (10 - 1)! + 9 * 9! / (10 - 2)! = 9 + 9 * 9 = 90\n\nNow, for every `i` in (`digsCount`..0] we get all the digits `j` smaller than `digits[i]` and that are not used in the higher digits of `N`. We calculate P(10 - `digsCount` + `i`, `i`). Zero cannot be highest digit, so we skip it (`j = (i == digsCount - 1) ? 1 : 0`). If `digits[i]` was used before  it means the original number `N` is not composed of unique digits only, and at this point we stop calculations.\n\n`i` = 2, `digits[i]` = 1, `j` = 1, `used` = [] (skip since `j` is not < `digits[i]`)\n\n`i` = 1, `digits[i]` = 1, `j` = 0, `used` = [1]\nSince two digits are used (0 and 1) we calculate permutation using 10 - 2 digits:\n`uniq` = `uniq` + P(10 - `digsCount` + `i`, `i`) = 90 + P(10-3+1, 1) = 90 + 8 = 98\n\n`i` = 1, `digits[i]` = 1, `j` = 1, `used` = [1] (skip since `j` is not < `digits[i]`)\n\nFinish calculations since `digits[i]` is in `used`. Since `digits` contains duplicates we do not increment `uniq`;\n\nReturn `N` - `uniq` = 112 - 98 = 14\n\n```java\n    public int numDupDigitsAtMostN(int N) {\n        int[] digits = new int[10];\n        int digsCount = getDigits(N, digits);\n\n        boolean[] used = new boolean[10];\n        boolean numUniq = true;\n        int uniq = initPermCount(digsCount);\n        for (int i = digsCount - 1; i >= 0; i--) {\n            for (int j = (i == digsCount - 1) ? 1 : 0; j < digits[i]; j++) {\n                if (!used[j]) {\n                    uniq += permCount(digsCount, digsCount - i);\n                }\n            }\n            if (used[digits[i]]) {\n                numUniq = false;\n                break;\n            }\n            used[digits[i]] = true;\n        }\n        if (numUniq) {\n            uniq++;\n        }\n        return N - uniq;\n    }\n\n    private int getDigits(int num, int[] digits) {\n        int i = 0;\n        while (num > 0) {\n            digits[i] = num % 10;\n            num /= 10;\n            i++;\n        }\n        return i;\n    }\n\n    private int initPermCount(int digsCount) {\n        int sum = 0;\n        for (int i = 1; i < digsCount; i++) {\n            sum += 9 * permCount(i, 1);\n        }\n        return sum;\n    }\n\n    private int permCount(int n, int reserved) {\n        int hi = 10 - reserved, lo = hi - (n - reserved);\n        int res = 1;\n        while (hi > lo) {\n            res *= hi--;\n        }\n        return res;\n    }\n```
[]