Skip to content

Commit f9b2891

Browse files
committed
no message
1 parent 647fa31 commit f9b2891

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
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/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+
}

0 commit comments

Comments
 (0)