#hey guys i need help with building algorithm#Python

1 messages · Page 1 of 1 (latest)

trail lodge
#

so i got this question from Leetcode:
The set [1, 2, 3, ..., n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order, we get the following sequence for n = 3:
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Example 1:
Input: n = 3, k = 3
Output: "213"
i thought about building a list from 1-n then insert n in each recusivly if number is bigger than the first in 1 insert first if smaller by 1 insert first and then chancing the position through slicing but i just cant make clear approach id appricate if someone explain... (ill do the code)

#

def permutation_sequence (n,k):
i = [[j] for j in range(1,n+1)]
k=0
if n < 0 :
return 0
i[j].append(n)
k+=1
print(i,k)
return permutation_sequence(n-1,k) thats my inital code idk if im on the right track T.T

subtle dawn
#

can you explain whats k?

trail lodge
#

we should return the k-th permutation like for n=3 we have 3! options = 6, suppose k=2 we want the second permutation