C++ 2 lines, lookup in precalculated array. Time and space O(1)

  • -1

    In this sort of tasks, it is possible to precalculate values and store them in either hash table or simple array, if we know that number of unique combinations has upper limit.

        int countNumbersWithUniqueDigits(int i) {
    	   int arr [] = {1,10,91,739,5275,32491,168571,712891,2345851,5611771,8877691};
    	   return i <= 10 ? arr[i] : arr[10];

Log in to reply

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