Skip to content

Commit 44a4412

Browse files
committed
no message
1 parent 2c73601 commit 44a4412

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/199_Binary_Tree_Right_Side_View.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import Foundation
1414
* }
1515
* }
1616
*/
17+
18+
// DFS
1719
class Solution {
1820
func rightSideView(_ root: TreeNode?) -> [Int] {
1921
guard var root = root else {
@@ -37,3 +39,31 @@ class Solution {
3739
rightSideViewDFSHelper(root.left, currentDepth + 1, &maxDepthSoFar, &rightView)
3840
}
3941
}
42+
43+
44+
// BFS with level size measurement
45+
class Solution {
46+
func rightSideView(_ root: TreeNode?) -> [Int] {
47+
guard let root = root else {
48+
return []
49+
}
50+
var result = [Int](), queue = [TreeNode](), currentLevelLength = 1
51+
queue.append(root)
52+
while !queue.isEmpty {
53+
var rightMostNode = Int.min, currentLevelLength = queue.count
54+
while currentLevelLength > 0 {
55+
let node = queue.removeFirst()
56+
rightMostNode = node.val
57+
currentLevelLength -= 1
58+
if let left = node.left {
59+
queue.append(left)
60+
}
61+
if let right = node.right {
62+
queue.append(right)
63+
}
64+
}
65+
result.append(rightMostNode)
66+
}
67+
return result
68+
}
69+
}

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/31_Next_Permutation.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
21
import Foundation
32

3+
// My first attempt
44
class Solution {
55
func nextPermutation(_ nums: inout [Int]) {
66
guard nums.count >= 2 else {
77
return
88
}
99

1010
for leftIdx in stride(from:(nums.count - 2), through:0, by:-1) {
11-
var leftNum = nums[leftIdx]
12-
var rightNum = nums[leftIdx + 1]
11+
let leftNum = nums[leftIdx], rightNum = nums[leftIdx + 1]
1312
if leftNum < rightNum {
1413
var swapIdx = leftIdx + 1
1514
for i in (swapIdx..<nums.count) {
@@ -25,3 +24,29 @@ class Solution {
2524
nums.reverse()
2625
}
2726
}
27+
28+
29+
// My second attempt
30+
class Solution {
31+
func nextPermutation(_ nums: inout [Int]) {
32+
guard nums.count > 1 else {
33+
return
34+
}
35+
for rightIdx in stride(from: nums.count - 1, through: 1, by: -1) {
36+
let rightNum = nums[rightIdx], leftNum = nums[rightIdx - 1]
37+
if rightNum > leftNum {
38+
var swapIdx = rightIdx, nextGraterNum = rightNum
39+
for j in swapIdx..<nums.count {
40+
if leftNum < nums[j] && nums[j] < nextGraterNum {
41+
swapIdx = j
42+
nextGraterNum = nums[j]
43+
}
44+
}
45+
nums.swapAt(rightIdx - 1, swapIdx)
46+
nums[rightIdx..<nums.count].sort()
47+
return
48+
}
49+
}
50+
nums.sort()
51+
}
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
// Video: https://www.youtube.com/watch?v=g9YQyYi4IQQ
4+
class Solution {
5+
func myPow(_ x: Double, _ n: Int) -> Double {
6+
if n == 0 {
7+
return 1
8+
}
9+
if n < 0 {
10+
return 1 / myPow(x, -n)
11+
}
12+
let half = myPow(x, n/2)
13+
if n % 2 == 0 {
14+
return half * half
15+
} else {
16+
return x * half * half
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)