diff --git a/climbing-stairs/WhiteHyun.swift b/climbing-stairs/WhiteHyun.swift new file mode 100644 index 000000000..23bc56147 --- /dev/null +++ b/climbing-stairs/WhiteHyun.swift @@ -0,0 +1,18 @@ +// +// 70. Climbing Stairs +// https://leetcode.com/problems/climbing-stairs/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/12. +// + +final class Solution { + func climbStairs(_ n: Int) -> Int { + var prevWays = 1 + var prevPrevWays = 1 + for _ in stride(from: 2, through: n, by: 1) { + (prevWays, prevPrevWays) = (prevPrevWays, prevWays + prevPrevWays) + } + return prevPrevWays + } +} diff --git a/maximum-depth-of-binary-tree/WhiteHyun.swift b/maximum-depth-of-binary-tree/WhiteHyun.swift new file mode 100644 index 000000000..e1fdf16a8 --- /dev/null +++ b/maximum-depth-of-binary-tree/WhiteHyun.swift @@ -0,0 +1,30 @@ +// +// 104. Maximum Depth of Binary Tree +// https://leetcode.com/problems/maximum-depth-of-binary-tree/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/12. +// + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ +final class Solution { + func maxDepth(_ node: TreeNode?) -> Int { + guard let node else { return 0 } + + return max(maxDepth(node.left), maxDepth(node.right)) + 1 + } +} diff --git a/meeting-rooms/WhiteHyun.swift b/meeting-rooms/WhiteHyun.swift new file mode 100644 index 000000000..6ecfffd15 --- /dev/null +++ b/meeting-rooms/WhiteHyun.swift @@ -0,0 +1,31 @@ +// +// 252. Meeting Rooms +// https://leetcode.com/problems/meeting-rooms/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/12. +// + + +/* +Definition of Interval: +class Interval { + var start: Int + var end: Int + init() { start = 0; end = 0; } + init(_ a: Int, _ b: Int) { start = a; end = b } +} + */ +final class Solution { + func canAttendMeetings(_ intervals: [Interval]) -> Bool { + let sortedIntervals = intervals.sorted { lhs, rhs in + lhs.start < rhs.start + } + + for i in sortedIntervals.indices.dropFirst() where sortedIntervals[i - 1].end > sortedIntervals[i].start { + return false + } + + return true + } +} diff --git a/same-tree/WhiteHyun.swift b/same-tree/WhiteHyun.swift new file mode 100644 index 000000000..ea8f17f73 --- /dev/null +++ b/same-tree/WhiteHyun.swift @@ -0,0 +1,34 @@ +// +// 100. Same Tree +// https://leetcode.com/problems/same-tree/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/12. +// + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ +extension TreeNode: Equatable { + public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool { + lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right + } +} + +final class Solution { + func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool { + p == q + } +} diff --git a/subtree-of-another-tree/WhiteHyun.swift b/subtree-of-another-tree/WhiteHyun.swift new file mode 100644 index 000000000..01fb08a05 --- /dev/null +++ b/subtree-of-another-tree/WhiteHyun.swift @@ -0,0 +1,37 @@ +// +// 572. Subtree of Another Tree +// https://leetcode.com/problems/subtree-of-another-tree/description/ +// Dale-Study +// +// Created by WhiteHyun on 2024/05/12. +// + +/** + * Definition for a binary tree node. + * public class TreeNode { + * public var val: Int + * public var left: TreeNode? + * public var right: TreeNode? + * public init() { self.val = 0; self.left = nil; self.right = nil; } + * public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; } + * public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) { + * self.val = val + * self.left = left + * self.right = right + * } + * } + */ +extension TreeNode: Equatable { + public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool { + lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right + } +} + +final class Solution { + func isSubtree(_ node: TreeNode?, _ subRoot: TreeNode?) -> Bool { + if node == subRoot { return true } + else if node?.left != nil, isSubtree(node?.left, subRoot) { return true } + else if node?.right != nil, isSubtree(node?.right, subRoot) { return true } + return false + } +}