Skip to content
This repository was archived by the owner on Sep 22, 2021. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update 0060 - Permutation_Sequence.py
after LeetCode success
  • Loading branch information
sourabmaity authored Oct 5, 2020
commit 069354d31461ea41766cb40c02b71ab13641ebf3
23 changes: 8 additions & 15 deletions LeetCode/0060 - Permutation_Sequence.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
from itertools import permutations
class Solution:
data = []

def getPermutation(self, n: int, k: int) -> str:
self.data = []
string = []
for i in range(1, n+1):
string.append(str(i))
self.permutations(string, 0, len(string))
return self.data[k-1]

def permutations(self, string, val, length):
if val == length - 1:
self.data.append("".join(string))
else:
for i in range(val, length):
string[val], string[i] = string[i], string[val]
self.permutations(string, val + 1, length)
string[val], string[i] = string[i], string[val]

string = list(permutations(string))
temp = string[k-1]
string = []
string.append("".join(temp))
return string[0]

user = Solution()
output = user.getPermutation(int(input("Input: n=")), int(input("k=")))
print("\nOutput: {}".format(output))