Skip to content

Commit 4c5c643

Browse files
author
Albert Hu
authored
Merge pull request #16 from alberthu16/day22
Day 22: permutations of a distinct array of numbers
2 parents a797ab8 + f86b6a6 commit 4c5c643

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

day22/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Question of the day: https://leetcode.com/problems/permutations/#/description
2+
3+
Given a collection of distinct numbers, return all possible permutations.
4+
5+
For example,
6+
[1,2,3] have the following permutations:
7+
8+
```python
9+
[
10+
[1,2,3],
11+
[1,3,2],
12+
[2,1,3],
13+
[2,3,1],
14+
[3,1,2],
15+
[3,2,1]
16+
]
17+
```
18+
19+
## Ideas
20+
21+
This is a pretty basic question but while I was working on a different question
22+
(which I'll save for tomorrow), I realized that the recursion for this problem
23+
didn't really come naturally to me.
24+
25+
A good way to think about it is to assume that I have the first element of the
26+
list, ready to go, and I have all possible permutations of the rest of the list,
27+
already calculated for me. In this situation, I'd just take that first element
28+
and insert it into each slot in each permutation, starting from 0, going to the
29+
end.
30+
31+
i.e.
32+
33+
If I had the permutations `[[2, 3], [3, 2]]` calculated already, all I need to
34+
do to include the `1` is to "insert" it into each possible location in each of
35+
the permutations, like this:
36+
37+
```python
38+
[1, 2, 3]
39+
[2, 1, 3]
40+
[2, 3, 1]
41+
[1, 3, 2]
42+
[3, 1, 2]
43+
[3, 2, 1]
44+
```
45+
46+
This solution is `O((spots for the first elem to go in) * (num perms of the remaining list))`
47+
where `(num perms of the remaining list)` is equal to
48+
`(spots for the first elem to go in) * (num perms of the remaining list)` recursively.
49+
So the runtime is `O(n!)`.
50+
51+
## Code
52+
53+
[Python](permutations.py)
54+
55+
## Follow up
56+
57+
This procedure has already been implemented in Python's built-in library:
58+
59+
```python
60+
from itertools import permutations
61+
62+
permutations([1, 2, 3])
63+
```
64+
65+
https://docs.python.org/2/library/itertools.html#itertools.permutations

day22/permutations.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def permutations(arr):
2+
allPermutations = list()
3+
4+
if len(arr) < 1:
5+
return [()]
6+
else:
7+
for i in xrange(len(arr)):
8+
for permutation in permutations(arr[1:]):
9+
allPermutations.append(permutation[:i] + (arr[0],) + permutation[i:])
10+
11+
return allPermutations
12+
13+
def main():
14+
assert set(permutations([])) == set([()])
15+
assert set(permutations([1])) == set([(1,)])
16+
assert set(permutations([1,2])) == set([(1, 2), (2, 1)])
17+
assert set(permutations([1,2,3])) == set([(1, 2, 3), (1, 3, 2), (2, 1, 3), (3, 1, 2), (2, 3, 1), (3, 2, 1)])
18+
19+
if __name__ == "__main__":
20+
main()

0 commit comments

Comments
 (0)