Skip to content

Commit daeec70

Browse files
committed
Merge branch 'master' of github.com:partho-maple/coding-interview-gym
* 'master' of github.com:partho-maple/coding-interview-gym: no message no message no message no message no message cleanup 408_Valid_Word_Abbreviation
2 parents 317c7ea + f9b2891 commit daeec70

9 files changed

+230
-21
lines changed

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/129_Sum_Root_to_Leaf_Numbers.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,32 @@ class Solution {
5454
return node.left == nil && node.right == nil
5555
}
5656
}
57+
58+
59+
// Second try
60+
class Solution {
61+
func sumNumbers(_ root: TreeNode?) -> Int {
62+
var currentPath = [Int](), currentSum = 0
63+
sumNumbersDFSHelper(root, currentPath, &currentSum)
64+
return currentSum
65+
}
66+
67+
func sumNumbersDFSHelper(_ root: TreeNode?, _ currentPath: [Int], _ currentSum: inout Int) {
68+
guard let root = root else {
69+
return
70+
}
71+
72+
if root.left == nil && root.right == nil {
73+
let currentPath = currentPath + [root.val]
74+
let currentNumStr = currentPath.reduce("") { $0 + String($1) }
75+
let currentNum = Int(currentNumStr)
76+
currentSum += currentNum!
77+
}
78+
if let left = root.left {
79+
sumNumbersDFSHelper(left, currentPath + [root.val], &currentSum)
80+
}
81+
if let right = root.right {
82+
sumNumbersDFSHelper(right, currentPath + [root.val], &currentSum)
83+
}
84+
}
85+
}

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/133_Clone_Graph.swift

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,23 @@ import Foundation
1515

1616
class Solution {
1717
func cloneGraph(_ node: Node?) -> Node? {
18-
guard let unWrappedNode = node else { return node}
19-
20-
var nodeDict = [Int: Node]()
21-
let clonedNode = self.cloneGraphHelper(unWrappedNode, &nodeDict)
22-
return clonedNode
18+
guard let node = node else { return node}
19+
var visited = [Int: Node]()
20+
return cloneGraphHelper(node, &visited)
2321
}
2422

25-
func cloneGraphHelper(_ node: Node, _ nodeDict: inout [Int: Node]) -> Node {
26-
if let existingNode = nodeDict[node.val] {
23+
func cloneGraphHelper(_ node: Node, _ visited: inout [Int: Node]) -> Node {
24+
if let existingNode = visited[node.val] {
2725
return existingNode
2826
}
2927

30-
var clonedNode = Node(node.val)
31-
nodeDict[node.val] = clonedNode
32-
node.neighbors.forEach {neighbor in
28+
visited[node.val] = Node(node.val)
29+
node.neighbors.forEach { neighbor in
3330
if let unWrappedNeighbor = neighbor {
34-
let clonedNeighbour = self.cloneGraphHelper(unWrappedNeighbor, &nodeDict)
35-
nodeDict[node.val]?.neighbors.append(clonedNeighbour)
31+
visited[node.val]?.neighbors.append(self.cloneGraphHelper(unWrappedNeighbor, &visited))
3632
}
3733
}
38-
return clonedNode
34+
return visited[node.val]!
3935
}
4036
}
4137

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/253_Meeting_Rooms_II.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,23 @@ class Solution {
4646
return occupiedRooms.count
4747
}
4848
}
49+
50+
51+
52+
// Improved Code
53+
class Solution {
54+
func minMeetingRooms(_ intervals: [[Int]]) -> Int {
55+
guard intervals.count > 1 else {
56+
return 1
57+
}
58+
let sortedInterval = intervals.sorted { $0[0] < $1[0] }
59+
var minHeap: Heap<Int> = Heap<Int>(sort: <)
60+
for i in 0..<sortedInterval.count {
61+
if let peak = minHeap.peek(), peak <= sortedInterval[i][0] {
62+
minHeap.remove()
63+
}
64+
minHeap.insert(sortedInterval[i][1])
65+
}
66+
return minHeap.count
67+
}
68+
}

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+
}

leetcode.com/swift/DS_ALGO_PRAC.playground/Sources/swift/339_Nested_List_Weight_Sum.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Foundation
2222
* public func getList() -> [NestedInteger]
2323
* }
2424
*/
25+
26+
// Old solution
2527
class Solution {
2628
func depthSum(_ nestedList: [NestedInteger]) -> Int {
2729
var wrightSum = 0
@@ -42,10 +44,23 @@ class Solution {
4244
}
4345
}
4446

45-
/*
46-
[[1,1],2,[1,1]]
4747

48-
1*2 + 1*2 + 1*2 +1*2 + 2*1
4948

50-
[[1,1],2,[1,1]]
51-
*/
49+
// New solution
50+
class Solution {
51+
func depthSum(_ nestedList: [NestedInteger]) -> Int {
52+
return depthSumDFSHelper(nestedList, 1)
53+
}
54+
55+
func depthSumDFSHelper(_ nestedList: [NestedInteger], _ currentDepth: Int) -> Int {
56+
var currentSum = 0
57+
for nInt in nestedList {
58+
if nInt.isInteger() {
59+
currentSum += (nInt.getInteger() * currentDepth)
60+
} else {
61+
currentSum += (depthSumDFSHelper(nInt.getList(), currentDepth + 1))
62+
}
63+
}
64+
return currentSum
65+
}
66+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Foundation
2+
3+
class Solution {
4+
func validWordAbbreviation(_ word: String, _ abbr: String) -> Bool {
5+
let wordArray = Array(word), abbrArray = Array(abbr)
6+
var wordPtr = 0, abbrPtr = 0, currentNumber = [String]()
7+
while wordPtr < wordArray.count && abbrPtr < abbrArray.count {
8+
if abbrArray[abbrPtr].isNumber {
9+
if currentNumber.isEmpty && String(abbrArray[abbrPtr]) == "0" {
10+
return false
11+
}
12+
currentNumber.append(String(abbrArray[abbrPtr]))
13+
abbrPtr += 1
14+
} else if currentNumber.count > 0 {
15+
let num = Int(currentNumber.reduce("") { $0 + $1 })
16+
wordPtr += num!
17+
currentNumber.removeAll()
18+
} else if wordArray[wordPtr] == abbrArray[abbrPtr] {
19+
wordPtr += 1
20+
abbrPtr += 1
21+
} else {
22+
return false
23+
}
24+
}
25+
26+
if currentNumber.count > 0 {
27+
let num = Int(currentNumber.reduce("") { $0 + $1 })
28+
wordPtr += num!
29+
currentNumber.removeAll()
30+
}
31+
return (wordPtr == wordArray.count && abbrPtr == abbrArray.count) ? true : false
32+
}
33+
}
34+
35+
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
3+
class Solution {
4+
func findClosestLeaf(_ root: TreeNode?, _ k: Int) -> Int {
5+
var graph = [Int:[Int]](), queue = [(TreeNode?, TreeNode)]() // (currentNode, parentNode)
6+
queue.append((root, TreeNode(Int.max)))
7+
8+
// Creates the graph.
9+
while !queue.isEmpty {
10+
let (node, parentNode) = queue.removeFirst()
11+
if let node = node {
12+
graph[node.val, default: [Int]()].append(parentNode.val)
13+
graph[parentNode.val, default: [Int]()].append(node.val)
14+
queue.append((node.left, node))
15+
queue.append((node.right, node))
16+
}
17+
}
18+
19+
var valQueue = [Int](), visited = Set<Int>()
20+
valQueue.append(k)
21+
while !valQueue.isEmpty {
22+
let currentNodeVal = valQueue.removeFirst()
23+
visited.insert(currentNodeVal)
24+
if let neighbours = graph[currentNodeVal] {
25+
if neighbours.count <= 1 && currentNodeVal != Int.max {
26+
return currentNodeVal
27+
}
28+
for i in 0..<neighbours.count {
29+
let neighbour = neighbours[i]
30+
if !visited.contains(neighbour) {
31+
valQueue.append(neighbour)
32+
}
33+
}
34+
} else {
35+
return currentNodeVal
36+
}
37+
}
38+
return -1
39+
}
40+
}

0 commit comments

Comments
 (0)