Generate 10-digit number using a phone keypad

后端 未结 12 917
遥遥无期
遥遥无期 2020-12-13 14:12

Given a phone keypad as shown below:

1 2 3
4 5 6
7 8 9
  0

How many different 10-digit numbers can be formed starting from 1? The constrain

12条回答
  •  既然无缘
    2020-12-13 14:37

    Sure it can be done in polynomial time. It's an excellent exercise in dynamic programming or memoization.

    Lets assume N (the number of digits) equals 10 for the example.

    Think of it recursively like this: How many numbers can I construct using 10 digits starting from 1?

    Answer is

    [number of 9-digit numbers starting from 8] +
    [number of 9-digit numbers starting from 6].
    

    So how many "9-digit numbers starting from 8" are there? Well,

    [number of 8-digit numbers starting from 1] +
    [number of 8-digit numbers starting from 3]
    

    and so on. Base case is reached when you get the question "How many 1-digit numbers are there starting from X" (and the answer is obviously 1).

    When it comes to complexity, the key observation is that you reuse previously computed solutions. That is for instance, the answer to "how many 5-digit numbers starting from 3" there are, can be used both when answering "how many 6-digit numbers are there starting from 8" AND "how many 6-digit numbers are there starting from 4". This reuse make the complexity collapse from exponential to polynomial.

    Let's take a closer look at the complexity of a dynamic programming solution:

    Such implementation would fill in a matrix in the following way:

    num[1][i] = 1, for all 0<=i<=9   -- there are one 1-digit number starting from X.
    
    for digits = 2...N
        for from = 0...9
            num[digits][from] = num[digits-1][successor 1 of from] +
                                num[digits-1][successor 2 of from] +
                                ...
                                num[digits-1][successor K of from]
    
    return num[N][1]                 -- number of N-digit numbers starting from 1.
    

    The algorithm simply fills the matrix one cell at a time, and the matrix is of dimension 10*N, and thus runs in linear time.


    Wrote it down from the top of my head, please correct me if there are any typos.

提交回复
热议问题