|
| 1 | +## [Factorial number system](http://en.wikipedia.org/wiki/Factorial_number_system#Permutations) |
1 | 2 |
|
2 |
| -## TODO |
3 |
| - * write down thinking |
| 3 | +It has to master this technology to find `kth` permutation or time limited. |
4 | 4 |
|
| 5 | +## Detail steps |
| 6 | + |
| 7 | +using wikipedia example |
| 8 | + |
| 9 | +2982th permutation of `[1, 2, 3, 4, 5, 6, 7]`. |
| 10 | + |
| 11 | +### Convert `k` to factorial based number |
| 12 | + |
| 13 | +``` |
| 14 | +k = 2982 |
| 15 | +n = 7 |
| 16 | +``` |
| 17 | + |
| 18 | +start form `n - 1` |
| 19 | + |
| 20 | +``` |
| 21 | +2982 / 6! = 4 and remainder 102 | 4 |
| 22 | +102 / 5! = 0 and remainder 102 | 0 |
| 23 | +102 / 4! = 4 and remainder 6 | 4 |
| 24 | +6 / 3! = 1 and remainder 0 | 1 |
| 25 | +0 / 2! = 0 and remainder 0 | 0 |
| 26 | +0 / 1! = 0 and remainder 0 | 0 |
| 27 | +0 / 0! = 0 and remainder 0 | 0 |
| 28 | +``` |
| 29 | + |
| 30 | +So, `2982(10base) = 4041000(!base)` |
| 31 | + |
| 32 | +### Recover factorial based number to permutation. |
| 33 | + |
| 34 | +``` |
| 35 | +
|
| 36 | +
|
| 37 | +
|
| 38 | +index: 0 1 2 3 4 5 6 |
| 39 | +chars: [1, 2, 3, 4, 5, 6, 7] |
| 40 | +final: [] |
| 41 | +
|
| 42 | +
|
| 43 | +number: 4041000 |
| 44 | + ^ |
| 45 | + | |
| 46 | +
|
| 47 | +Move chars[4] to the end of final |
| 48 | +
|
| 49 | +
|
| 50 | +chars: [1, 2, 3, 4, 5, 6, 7] BEFORE |
| 51 | +index: 0 1 2 3 4 5, 6 |
| 52 | +final: [5] |
| 53 | +chars: [1, 2, 3, 4, 6, 7] AFTER |
| 54 | +
|
| 55 | +
|
| 56 | +number: 4041000 |
| 57 | + ^ |
| 58 | + | |
| 59 | +
|
| 60 | +Move chars[0] to the end of final |
| 61 | +
|
| 62 | +chars: [1, 2, 3, 4, 6, 7] BEFORE |
| 63 | +index: 0 1 2 3 4 5 |
| 64 | +final: [5, 1] |
| 65 | +chars: [2, 3, 4, 6, 7] AFTER |
| 66 | +
|
| 67 | +number: 4041000 |
| 68 | + ^ |
| 69 | + | |
| 70 | +
|
| 71 | +Move chars[4] to the end of final |
| 72 | +
|
| 73 | +chars: [2, 3, 4, 6, 7] BEFORE |
| 74 | +index: 0 1 2 3 4 |
| 75 | +final: [5, 1, 7] |
| 76 | +chars: [2, 3, 4, 6] AFTER |
| 77 | +
|
| 78 | +
|
| 79 | +
|
| 80 | +number: 4041000 |
| 81 | + ^ |
| 82 | + | |
| 83 | +
|
| 84 | +Move chars[1] to the end of final |
| 85 | +
|
| 86 | +chars: [2, 3, 4, 6] BEFORE |
| 87 | +index: 0 1 2 3 |
| 88 | +final: [5, 1, 7, 3] |
| 89 | +chars: [2, 4, 6] AFTER |
| 90 | +
|
| 91 | +
|
| 92 | +
|
| 93 | +number: 4041000 |
| 94 | + ^ |
| 95 | + | |
| 96 | +
|
| 97 | +Move chars[0] to the end of final |
| 98 | +
|
| 99 | +chars: [2, 4, 6] BEFORE |
| 100 | +index: 0 1 2 |
| 101 | +final: [5, 1, 7, 3, 2] |
| 102 | +chars: [4, 6] AFTER |
| 103 | +
|
| 104 | +
|
| 105 | +
|
| 106 | +number: 4041000 |
| 107 | + ^ |
| 108 | + | |
| 109 | +
|
| 110 | +Move chars[0] to the end of final |
| 111 | +
|
| 112 | +chars: [4, 6] BEFORE |
| 113 | +index: 0 1 |
| 114 | +final: [5, 1, 7, 3, 2, 4] |
| 115 | +chars: [6] AFTER |
| 116 | +
|
| 117 | +
|
| 118 | +number: 4041000 |
| 119 | + ^ |
| 120 | + | |
| 121 | +
|
| 122 | +Move chars[0] to the end of final |
| 123 | +
|
| 124 | +chars: [6] BEFORE |
| 125 | +index: 0 |
| 126 | +final: [5, 1, 7, 3, 2, 4, 6] |
| 127 | +chars: [] AFTER |
| 128 | +
|
| 129 | +
|
| 130 | +``` |
| 131 | + |
| 132 | +The 2982th permutation of `[1, 2, 3, 4, 5, 6, 7]` is `[5, 1, 7, 3, 2, 4, 6]` |
0 commit comments