|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +/** |
| 4 | + * A chess knight can move as indicated in the chess diagram below: |
| 5 | + * |
| 6 | + * https://assets.leetcode.com/uploads/2018/10/12/knight.png |
| 7 | + * |
| 8 | + * |---|---|---| |
| 9 | + * | 1 | 2 | 3 | |
| 10 | + * |---|---|---| |
| 11 | + * | 4 | 5 | 6 | |
| 12 | + * |---|---|---| |
| 13 | + * | 7 | 8 | 9 | |
| 14 | + * |---|---|---| |
| 15 | + * | x | 0 | x | |
| 16 | + * |---|---|---| |
| 17 | + * |
| 18 | + * This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops. |
| 19 | + * Each hop must be from one key to another numbered key. |
| 20 | + * |
| 21 | + * Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total. |
| 22 | + * |
| 23 | + * How many distinct numbers can you dial in this manner? |
| 24 | + * |
| 25 | + * Since the answer may be large, output the answer modulo 10^9 + 7. |
| 26 | + * |
| 27 | + * Note: |
| 28 | + * * 1 <= N <= 5000 |
| 29 | + */ |
| 30 | + |
| 31 | +public class _935 { |
| 32 | + /* |
| 33 | + * The intuition is to calculate the number of ways |
| 34 | + * we can reach a key k after i hops, based on the number of ways we can reach keys x after i-1 hops |
| 35 | + * s.t. the knight can move from x to k in one move |
| 36 | + * For example, |
| 37 | + * We can reach 6 in 3 ways after 1 hop (1 -> 6, 7 -> 6 or 0 -> 6) |
| 38 | + * We can reach 8 in 2 ways after 1 hop (1 -> 8 or 3 -> 8) |
| 39 | + * Thus, we can reach 1 in 5 ways after 2 hops: |
| 40 | + * . 1. 1 -> 6 -> 1 |
| 41 | + * . 2. 7 -> 6 -> 1 |
| 42 | + * . 3. 0 -> 6 -> 1 |
| 43 | + * 4. 1 -> 8 -> 1 |
| 44 | + * 5. 3 -> 8 -> 1 |
| 45 | + */ |
| 46 | + public static class Solution1 { |
| 47 | + private static final int MOD = 1000_000_007; |
| 48 | + |
| 49 | + // whereFromHere[i] is an array of keys that can be reached from the ith digit |
| 50 | + private static final int[][] whereFromHere = { |
| 51 | + {4, 6}, {6, 8}, {7, 9}, {4, 8}, // 0, 1, 2, 3 |
| 52 | + {3, 9, 0}, {}, {1, 7, 0}, // 4, 5, 6 |
| 53 | + {2, 6}, {1, 3}, {2, 4} // 7, 8, 9 |
| 54 | + }; |
| 55 | + |
| 56 | + public int knightDialer(int N) { |
| 57 | + // a[i] is the number of ways we can end up on the ith digit |
| 58 | + // The initial array is for N = 1, i.e. for 0 hops. |
| 59 | + long[] a = new long[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; |
| 60 | + |
| 61 | + // Simulate N - 1 hops |
| 62 | + for (int i = 0; i < N - 1; ++i) { |
| 63 | + long[] tmp = new long[10]; |
| 64 | + |
| 65 | + // For each digit |
| 66 | + for (int j = 0; j < 10; j++) { |
| 67 | + // Which other digits can we reach? |
| 68 | + for (int k : whereFromHere[j]) { |
| 69 | + tmp[j] = (tmp[j] + a[k]) % MOD; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Sanity checks based on symmetry of the keypad |
| 74 | + assert tmp[1] == tmp[3]; |
| 75 | + assert tmp[4] == tmp[6]; |
| 76 | + assert tmp[7] == tmp[9]; |
| 77 | + |
| 78 | + a = tmp; |
| 79 | + } |
| 80 | + |
| 81 | + long ans = 0; |
| 82 | + for (long k : a) { |
| 83 | + ans = (ans + k) % MOD; |
| 84 | + } |
| 85 | + |
| 86 | + return (int) ans; |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments