Skip to content

Commit cb5a264

Browse files
author
Partho Biswas
committed
60. Permutation Sequence
1 parent c555e7d commit cb5a264

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
611611
|20| [1088. Confusing Number II](https://tinyurl.com/tb7w75j)| [Python](https://tinyurl.com/wu6rdaw/1088_Confusing_Number_II.py), [Swift](https://tinyurl.com/wuja3c4/1088_Confusing_Number_II.swift) | [Art 1](https://tinyurl.com/wby5h93), [Ref 1](https://tinyurl.com/w54thz5) | Hard | Very interesting |
612612
|21| [465. Optimal Account Balancing](https://tinyurl.com/smsbk4t)| [Python](https://tinyurl.com/wu6rdaw/465_Optimal_Account_Balancing.py), [Swift](https://tinyurl.com/wuja3c4/465_Optimal_Account_Balancing.swift) | **[Art 1](https://tinyurl.com/txuvptu)** | Hard | Loved the question, awsome real life description |
613613
|22| [1079. Letter Tile Possibilities](https://tinyurl.com/r5xjymk) | [Python](https://tinyurl.com/wu6rdaw/1079_Letter_Tile_Possibilities.py), [Swift](https://tinyurl.com/wuja3c4/1079_Letter_Tile_Possibilities.swift) | --- | Medium |📌 Could be done using DFS, backtracking |
614+
|23| **[60. Permutation Sequence](https://tinyurl.com/yz98ms7h)** | [Python](https://tinyurl.com/wu6rdaw/60_Permutation_Sequence.py), [Swift](https://tinyurl.com/wuja3c4/60_Permutation_Sequence.swift) | **[Art 1](https://tinyurl.com/ybvcswyd)** | Medium | 📌 What an awesome question and smart solution. loved it. |
614615

615616
</p>
616617
</details>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def getPermutation(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: str
7+
"""
8+
factorial = {1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: (5040), 8: (40320), 9: (362880)}
9+
digits = [str(x) for x in range(1, n + 1)]
10+
11+
number = []
12+
currentDigitCount = 1
13+
currentK = k - 1
14+
15+
while currentDigitCount < n:
16+
chosenDigitIndex, currentK = divmod(currentK, factorial[n - currentDigitCount])
17+
number.append(digits.pop(chosenDigitIndex))
18+
currentDigitCount += 1
19+
20+
number.append(digits[0])
21+
return ''.join(number)

0 commit comments

Comments
 (0)