Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Python/PermutationSequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#Problem link - https://leetcode.com/problems/permutation-sequence/
class Solution(object):
def getPermutation(self, n, k):
"""
:type n: int
:type k: int
:rtype: str
"""
# let permutations with first identical num be a block
# target in (k - 1) / (n - 1)! block
remain = range(1, n + 1)
if k <= 1:
return ''.join(str(t) for t in remain)
total = 1
for num in remain[:-1]:
total *= num
res = self.do_getPermutation(remain, total, n - 1, k - 1)
return ''.join(str(t) for t in res)


def do_getPermutation(self, remain, curr, n, k):
if n == 0 or k <= 0 or curr == 0:
return remain
# which block
step = k / curr
# remain k value
k %= curr
curr /= n
res = [remain[step]] + self.do_getPermutation(remain[:step] + remain[step + 1:], curr, n - 1, k)
return res

if __name__ == '__main__':
s = Solution()
print s.getPermutation(3, 2)
# print s.getPermutation(2, 2)