Skip to content

Commit 94fdc53

Browse files
authored
Merge pull request soapyigu#243 from WAMaker/master
[Math] Add a solution to Permutation Sequence
2 parents 6e94fdc + 8785cb2 commit 94fdc53

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

Math/PermutationSequence.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/permutation-sequence/
3+
* Primary idea: Iterate and change the array from last to the first
4+
*
5+
* Time Complexity: O(n^2), Space Complexity: O(1)
6+
*/
7+
8+
class PermutationSequence {
9+
func getPermutation(_ n: Int, _ k: Int) -> String {
10+
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
11+
12+
var factorial = 1
13+
for i in 1 ..< n {
14+
factorial *= i
15+
}
16+
17+
var result = ""
18+
var k = k
19+
var divisor = n - 1
20+
21+
for i in 0 ..< n {
22+
for (index, number) in numbers.enumerated() {
23+
if k > factorial {
24+
k -= factorial
25+
} else {
26+
result += "\(number)"
27+
numbers.remove(at: index)
28+
break
29+
}
30+
}
31+
if divisor > 1 {
32+
factorial /= divisor
33+
divisor -= 1
34+
}
35+
}
36+
37+
return result
38+
}
39+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 283 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 284 completed solutions. Note: questions with &hearts; mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -320,6 +320,7 @@
320320
[Counting Bits](https://leetcode.com/problems/counting-bits/)| [Swift](./Math/CountingBits.swift)| Medium| O(n)| O(n)|
321321
[K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/)| [Swift](./Math/KthSmallestLexicographicalOrder.swift)| Hard| O(n)| O(1)|
322322
[Gary Code](https://leetcode.com/problems/gray-code/)| [Swift](./Math/GaryCode.swift)| Medium| O(n)| O(2^n)|
323+
[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)| [Swift](./Math/PermutationSequence.swift)| Medium| O(n^2)| O(1)|
323324

324325
## Search
325326
| Title | Solution | Difficulty | Time | Space |
@@ -801,7 +802,7 @@
801802
| [Swift](./DP/UniquePathsII.swift) | 63 | [Unique Paths II](https://oj.leetcode.com/problems/unique-paths-ii/) | Medium |
802803
| [Swift](./DP/UniquePaths.swift) | 62 | [Unique Paths](https://oj.leetcode.com/problems/unique-paths/) | Medium |
803804
| [Swift](./LinkedList/RotateList.swift) | 61 | [Rotate List](https://oj.leetcode.com/problems/rotate-list/) | Medium |
804-
| | 60 | [Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/) | Medium |
805+
| [Swift](./Math/PermutationSequence.swift) | 60 | [Permutation Sequence](https://oj.leetcode.com/problems/permutation-sequence/) | Medium |
805806
| [Swift](./Array/SpiralMatrixII.swift) | 59 | [Spiral Matrix II](https://oj.leetcode.com/problems/spiral-matrix-ii/) | Medium |
806807
| [Swift](./String/LengthLastWord.swift) | 58 | [Length of Last Word](https://oj.leetcode.com/problems/length-of-last-word/) | Easy |
807808
| [Swift](./Sort/InsertInterval.swift) | 57 | [Insert Interval](https://oj.leetcode.com/problems/insert-interval/) | Hard |

0 commit comments

Comments
 (0)