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

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented May 12, 2024

70. Climbing Stairs

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n)$
  • Space Complexity: O(n) $O(1)$

Explanation ๐Ÿ“

DP๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ์Šคํ… ๋˜๋Š” ๋‘ ์Šคํ…์œผ๋กœ ๊ฑด๋„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๊ณ ๋ คํ•˜์—ฌ n๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ๊นŒ์ง€ n - 1๋ฒˆ์งธ ๊ณ„๋‹จ๊ณผ n - 2 ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์„ ๋”ํ•˜์—ฌ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

@SamTheKorean ๋‹˜์˜ ์•„์ด๋””์–ด๋กœ n ๋ฒˆ์งธ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๊ธฐ ์œ„ํ•ด n-1๋ฒˆ์งธ์˜ ํšŸ์ˆ˜์™€ n-2๋ฒˆ์งธ์˜ ํšŸ์ˆ˜๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ, n๋งŒํผ์˜ ๊ณต๊ฐ„์„ ์ „๋ถ€ ํ• ๋‹นํ•  ํ•„์š” ์—†์ด 1๋ถ€ํ„ฐ n๊นŒ์ง€ ์ฐจ๊ทผ์ฐจ๊ทผ ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ–ˆ์Šต๋‹ˆ๋‹ค.

100. Same Tree

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n)$
  • Space Complexity: $O(n)$

Explanation ๐Ÿ“

Swift์—๋Š” Equatable์ด๋ผ๋Š” protocol(=interface)๊ฐ€ ์žˆ๋Š”๋ฐ์š”. ์ด protocol์„ ์ค€์ˆ˜ํ•˜๋Š” ๊ฐ์ฒด๋Š” ์„œ๋กœ๋ฅผ ๋น„๊ตํ•  ๋•Œ ==๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ €๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์—ฌ recursiveํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

// extension์€ Swift์˜ ์˜ˆ์•ฝ์–ด ์ค‘ ํ•˜๋‚˜๋กœ, ์ˆ˜ํ‰์ ํ™•์žฅ์„ ์ผ์ปซ์Šต๋‹ˆ๋‹ค.
// ๊ฐ์ฒด๋งˆ๋‹ค protocol(=Interface)๋ฅผ ์ค€์ˆ˜์‹œํ‚ค๊ณ  ์‹ถ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
extension TreeNode: Equatable {
  public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool {
    lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right // return ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ
  }
}

final class Solution {
  func isSameTree(_ p: TreeNode?, _ q: TreeNode?) -> Bool {
    p == q  
  }
}

104. Maximum Depth of Binary Tree

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n)$
  • Space Complexity: $O(n)$

Explanation ๐Ÿ“

์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ์™€ ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ์˜ depth๊ฐ€ ๋‹ค๋ฅผ ์ˆ˜๋„ ์žˆ๊ธฐ์— ๊ฐ ์ž์‹ ๋…ธ๋“œ์—๊ฒŒ maxDepth๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊นŠ์ด๋ฅผ ๊ตฌํ•œ ํ›„ ์ตœ๋Œ“๊ฐ’์„ ๊ฐ€์ง€๊ณ  +1ํ•˜์—ฌ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
์žฌ๊ท€๋กœ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

252. Meeting Rooms

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n log n)$
  • Space Complexity: $O(n)$

Explanation ๐Ÿ“

๊ฐ ์‹œ๊ฐ„๋Œ€๊ฐ€ ๊ฒน์น˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋๋‚˜๋Š” ์‹œ๊ฐ„๊ณผ ์‹œ์ž‘ํ•˜๋Š” ์‹œ๊ฐ„ ์‚ฌ์ด์˜ ๊ฐญ์ด 0์ด์ƒ ์กด์žฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์„œ๋กœ ๊ฒน์ณ์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ Interval ์‹œ์ž‘ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ ํ›„ ๊ธฐ์ค€ ์š”์†Œ์˜ start ์‹œ๊ฐ„๊ณผ ์ด์ „ ์š”์†Œ์˜ end ์‹œ๊ฐ„์ด ๊ฒน์น˜๋Š”์ง€๋ฅผ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

572. Subtree of Another Tree

Complexities ๐Ÿ“ˆ

  • Time Complexity: $O(n * m)$
  • Space Complexity: $O(h)$

Explanation ๐Ÿ“

100. Same Tree์—์„œ ํ’€์—ˆ๋˜ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

node๊ฐ€ subTree์™€ ๋™์ผํ•œ์ง€๋ฅผ ๋ณธ ๋’ค,
์•„๋‹ˆ๋ผ๋ฉด node์˜ ์™ผ์ชฝ์ž์‹๋…ธ๋“œ๋‚˜ ์˜ค๋ฅธ์ชฝ ์ž์‹๋…ธ๋“œ์™€ subTree๋ฅผ ๋น„๊ตํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์žฌ๊ท€๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

@leokim0922 leokim0922 left a comment

Choose a reason for hiding this comment

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

๋ฒŒ์จ ๋‹ค ํ’€์œผ์…จ๋‹ค๋‹ˆ..ใ…Žใ…Ž 1๋“ฑ ์ถ•ํ•˜๋“œ๋ฆฝ๋‹ˆ๋‹ค

@SamTheKorean
Copy link
Contributor

PR ์ •๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ์ž˜ํ•˜์‹œ๋„ค์š”. ๋‹ค์Œ์—๋Š” ์ €๋„ ์ฐธ๊ณ ํ•ด์„œ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

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

Choose a reason for hiding this comment

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

Space Complexity๋ฅผ $O(1)$๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๋ชฐ๋ž๋„ค์š” ใ…Žใ…Ž
์•„์ด๋””์–ด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๊ตฌํ˜„ํ•ด๋ณด๊ณ  PR ๋‚ด์šฉ๋„ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

์ €๋„ ์ฒ˜์Œ์— Hyun๋‹˜๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค๊ฐ€ ๋‹ค๋ฅธ ๋ถ„๋“ค๊ป˜ ํ”ผ๋“œ๋ฐฑ ๋ฐ›๊ณ  ๊ณ ์ณค์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

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

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

Comment on lines +24 to +28
extension TreeNode: Equatable {
public static func == (lhs: TreeNode, rhs: TreeNode) -> Bool {
lhs.val == rhs.val && lhs.left == rhs.left && lhs.right == rhs.right
}
}
Copy link
Member

Choose a reason for hiding this comment

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

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

@DaleSeo DaleSeo merged commit 6d518d9 into DaleStudy:main May 13, 2024
@WhiteHyun WhiteHyun deleted the whitehyun/week3 branch July 19, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

4 participants