Skip to content

Commit 6d518d9

Browse files
authored
Merge pull request #67 from WhiteHyun/whitehyun/week3
[Hyun] Week 3 Solution Explanation
2 parents d769f97 + 31ddda8 commit 6d518d9

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed

climbing-stairs/WhiteHyun.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// 70. Climbing Stairs
3+
// https://leetcode.com/problems/climbing-stairs/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
final class Solution {
10+
func climbStairs(_ n: Int) -> Int {
11+
var prevWays = 1
12+
var prevPrevWays = 1
13+
for _ in stride(from: 2, through: n, by: 1) {
14+
(prevWays, prevPrevWays) = (prevPrevWays, prevWays + prevPrevWays)
15+
}
16+
return prevPrevWays
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// 104. Maximum Depth of Binary Tree
3+
// https://leetcode.com/problems/maximum-depth-of-binary-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
final class Solution {
25+
func maxDepth(_ node: TreeNode?) -> Int {
26+
guard let node else { return 0 }
27+
28+
return max(maxDepth(node.left), maxDepth(node.right)) + 1
29+
}
30+
}

meeting-rooms/WhiteHyun.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// 252. Meeting Rooms
3+
// https://leetcode.com/problems/meeting-rooms/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
10+
/*
11+
Definition of Interval:
12+
class Interval {
13+
var start: Int
14+
var end: Int
15+
init() { start = 0; end = 0; }
16+
init(_ a: Int, _ b: Int) { start = a; end = b }
17+
}
18+
*/
19+
final class Solution {
20+
func canAttendMeetings(_ intervals: [Interval]) -> Bool {
21+
let sortedIntervals = intervals.sorted { lhs, rhs in
22+
lhs.start < rhs.start
23+
}
24+
25+
for i in sortedIntervals.indices.dropFirst() where sortedIntervals[i - 1].end > sortedIntervals[i].start {
26+
return false
27+
}
28+
29+
return true
30+
}
31+
}

same-tree/WhiteHyun.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 100. Same Tree
3+
// https://leetcode.com/problems/same-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
extension TreeNode: Equatable {
25+
public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool {
26+
lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right
27+
}
28+
}
29+
30+
final class Solution {
31+
func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
32+
p == q
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// 572. Subtree of Another Tree
3+
// https://leetcode.com/problems/subtree-of-another-tree/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/05/12.
7+
//
8+
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* public var val: Int
13+
* public var left: TreeNode?
14+
* public var right: TreeNode?
15+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
16+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
17+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
18+
* self.val = val
19+
* self.left = left
20+
* self.right = right
21+
* }
22+
* }
23+
*/
24+
extension TreeNode: Equatable {
25+
public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool {
26+
lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right
27+
}
28+
}
29+
30+
final class Solution {
31+
func isSubtree(_ node: TreeNode?, _ subRoot: TreeNode?) -> Bool {
32+
if node == subRoot { return true }
33+
else if node?.left != nil, isSubtree(node?.left, subRoot) { return true }
34+
else if node?.right != nil, isSubtree(node?.right, subRoot) { return true }
35+
return false
36+
}
37+
}

0 commit comments

Comments
 (0)