-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
39 lines (31 loc) · 1 KB
/
Copy pathsolution.py
File metadata and controls
39 lines (31 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import json
import time
def factorial(n):
_factorial = 1
for i in range(n, 0, -1):
_factorial *= i
return _factorial
def nth_lexico_perm(n: int, chars):
"""
Find nth lexicographical permutation
of given characters.
"""
_chars = sorted(chars)
nth_permutation = []
while n > 0:
# number of all permutations minus the
# first character
sub_perm_count = factorial(len(_chars)-1)
next_char_idx = n // sub_perm_count
n -= sub_perm_count * next_char_idx
nth_permutation.append(_chars.pop(next_char_idx))
return nth_permutation + _chars
def solution() -> int:
perm = nth_lexico_perm(
1000001, ['0','1','2','3','4','5','6','7','8','9'])
return int(''.join(perm))
cpu_s, wall_s = time.process_time(), time.time()
result = solution()
cpu_e, wall_e = time.process_time(), time.time()
cpu_time, wall_time = cpu_e - cpu_s, wall_e - wall_s
print(json.dumps({"solution": result, "cpu": cpu_time, "wall": wall_time}))