-
-
Notifications
You must be signed in to change notification settings - Fork 248
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
376840a
feat: Add solution for LeetCode problem 104
WhiteHyun 4274f49
feat: Add solution for LeetCode problem 100
WhiteHyun 6dc43b9
feat: Add solution for LeetCode problem 572
WhiteHyun fac1fba
feat: Add solution for LeetCode problem 70
WhiteHyun 1552d84
feat: Add solution for LeetCode problem 252
WhiteHyun 83f6075
add: Add EOF line
WhiteHyun 31ddda8
feat: Optimize space complexity in climbStairs function
WhiteHyun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
lhs.start < rhs.start | ||
} | ||
|
||
for i in sortedIntervals.indices.dropFirst() where sortedIntervals[i - 1].end > sortedIntervals[i].start { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ค์ํํธ๋ ์ ๋ ฌํ๋ ค๋ฉด ์ฌ๋ฌ๊ฐ์ง๋ฅผ ์ง์ ๋ช ์ํด์ผ ํ๋๊ตฐ์ใ ใ
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swift
์ธ์ด์ ํน์ง์ด๋ผ๊ธฐ ๋ณด๋ค๋ ๋ณ๋๋ก ๋ง๋ ๊ฐ์ฒด์ ์ ๋ ฌ ๊ธฐ์ค์ ์ปดํ์ผ๋ฌ๊ฐ ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ ์ง์ ์๋ ค์ฃผ์ด์ผ ํฉ๋๋ค. ์ด๋ ๋ค๋ฅธ ์ธ์ด๋ ๋ง์ฐฌ๊ฐ์ง์์. ใ ใ ํ์ด์ฌ:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ง์ ๋ง๋์ ๊ฐ์ฒด๋ผ ๊ทธ๋ฌ๊ตฐ์. ์์ ์ฝ๋์ ์ดํด๊ฐ ๋ถ์กฑํ๋ค์ใ ใ ์ค๋ช ๊ฐ์ฌํฉ๋๋ค!