Skip to content

[Hyun] Week 3 Solution Explanation #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions climbing-stairs/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
30 changes: 30 additions & 0 deletions maximum-depth-of-binary-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions meeting-rooms/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์Šค์œ„ํ”„ํŠธ๋Š” ์ •๋ ฌํ•˜๋ ค๋ฉด ์—ฌ๋Ÿฌ๊ฐ€์ง€๋ฅผ ์ง์ ‘ ๋ช…์‹œํ•ด์•ผ ํ•˜๋Š”๊ตฐ์š”ใ…Žใ…Ž

Copy link
Member Author

@WhiteHyun WhiteHyun May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift ์–ธ์–ด์˜ ํŠน์ง•์ด๋ผ๊ธฐ ๋ณด๋‹ค๋Š” ๋ณ„๋„๋กœ ๋งŒ๋“  ๊ฐ์ฒด์˜ ์ •๋ ฌ ๊ธฐ์ค€์„ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋‹ค๋ฅธ ์–ธ์–ด๋„ ๋งˆ์ฐฌ๊ฐ€์ง€์—์š”. ใ…Žใ…Ž

ํŒŒ์ด์ฌ:

class Test:
    a = 0
    b = 0

array = [Test(), Test()]
array.sort() # โŒ TypeError: '<' not supported between instances of 'Test' and 'Test'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ง์ ‘ ๋งŒ๋“œ์‹  ๊ฐ์ฒด๋ผ ๊ทธ๋žฌ๊ตฐ์š”. ์œ„์— ์ฝ”๋“œ์— ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ–ˆ๋„ค์š”ใ…Žใ…Ž ์„ค๋ช… ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

lhs.start < rhs.start
}

for i in sortedIntervals.indices.dropFirst() where sortedIntervals[i - 1].end > sortedIntervals[i].start {
return false
}

return true
}
}
34 changes: 34 additions & 0 deletions same-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
37 changes: 37 additions & 0 deletions subtree-of-another-tree/WhiteHyun.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
Comment on lines +24 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ œ์–ด์„œ ์ฃผ์–ด์ง„ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๋‹ค๋‹ˆ ์‹ ์„ ํ•˜๊ตฐ์š”!


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