mirror of
https://github.com/dholerobin/Lecture_Notes.git
synced 2025-03-16 06:10:00 +00:00
Update 2.md
This commit is contained in:
parent
6ca41be67e
commit
872edc4dea
@ -109,12 +109,12 @@ $\dfrac{k}{(n-1)!}$ will give us the index of the first digit. Remove that digit
|
|||||||
def get_perm(A, k):
|
def get_perm(A, k):
|
||||||
perm = []
|
perm = []
|
||||||
while A:
|
while A:
|
||||||
# get the index of current digit
|
# get the index of current digit
|
||||||
div = factorial(len(A)-1)
|
div = factorial(len(A)-1)
|
||||||
i, k = divmod(k, div)
|
i, k = divmod(k, div)
|
||||||
perm.append(A[i])
|
perm.append(A[i])
|
||||||
# remove handled number
|
# remove handled number
|
||||||
del A[index]
|
del A[index]
|
||||||
return perm
|
return perm
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -125,9 +125,10 @@ Sorted Permutation Rank (Optional)
|
|||||||
> Given S, find the rank of the string amongst its permutations sorted lexicographically.
|
> Given S, find the rank of the string amongst its permutations sorted lexicographically.
|
||||||
Assume that no characters are repeated.
|
Assume that no characters are repeated.
|
||||||
|
|
||||||
```python
|
```
|
||||||
Input : 'acb'
|
Input : 'acb'
|
||||||
Output : 2
|
Output : 2
|
||||||
|
Explanation:
|
||||||
The order permutations with letters 'a', 'c', and 'b' :
|
The order permutations with letters 'a', 'c', and 'b' :
|
||||||
abc
|
abc
|
||||||
acb
|
acb
|
||||||
|
Loading…
x
Reference in New Issue
Block a user