Skip to content

Commit f945e88

Browse files
committed
commit changes to gh-pages aug 30
1 parent 614bb99 commit f945e88

File tree

2 files changed

+132
-3
lines changed

2 files changed

+132
-3
lines changed
Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,132 @@
1+
## [Factorial number system](http://en.wikipedia.org/wiki/Factorial_number_system#Permutations)
12

2-
## TODO
3-
* write down thinking
3+
It has to master this technology to find `kth` permutation or time limited.
44

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]`

permutation-sequence/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: solution
33
title: Permutation Sequence
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-29 20:57:36 +0800
5+
eaten: true
56
---
67
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
78
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

0 commit comments

Comments
 (0)