Skip to content
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

[Hyun] Week 2 Solution Explanation #47

Merged
merged 5 commits into from
May 8, 2024
Merged

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented May 4, 2024

20. Valid Parentheses

Complexities πŸ“ˆ

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

Explanation πŸ“

ASCII 좜λ ₯ 확인 Swift의 &- μ—°μ‚° κ³Όμ •
Screenshot 2024-05-04 at 4 25 55β€―PM image

각 λ¬Έμžμ—΄μ„ μˆœνšŒν•˜μ—¬ μ—΄λ¦° κ΄„ν˜Έλ©΄ μŠ€νƒμ— λ„£κ³ , λ‹«νžŒ κ΄„ν˜Έλ©΄ μ•Œλ§žλŠ” κ΄„ν˜ΈμΈμ§€ ν™•μΈν•˜κ³  μ§€μš°λŠ” μž‘μ—…μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.
κ΄„ν˜Έκ°€ μ•Œλ§žμ€μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄ ASCII value의 차이λ₯Ό ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€.

각 κ΄„ν˜Έλ³„ ASCIIλ₯Ό 좜λ ₯ν–ˆλ”λ‹ˆ μ„œλ‘œ 값이 μ΅œλŒ€ 2차이가 λ‚˜λŠ” 것을 λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ λ‹«νžŒ κ΄„ν˜Έλ₯Ό λ°œκ²¬ν–ˆμ„ 경우 ASCIIκ°’μ˜ 차이λ₯Ό μ£Όμ–΄ ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.
overflowκ°€ λ°œμƒν•  μ—Όλ €κ°€ μžˆμ–΄μ„œ Swiftμ—μ„œ μ œκ³΅ν•˜λŠ” μ˜€λ²„ν”Œλ‘œμš° 연산인 &λ₯Ό μ΄μš©ν–ˆμŠ΅λ‹ˆλ‹€.
&-λ₯Ό μ΄μš©ν•˜λ©΄ λΊ„μ…ˆμ„ ν•˜λ˜, μ˜€λ²„ν”Œλ‘œμš°λ₯Ό λ¬΄μ‹œν•©λ‹ˆλ‹€.

21. Merge Two Sorted Lists

Complexities πŸ“ˆ

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

Explanation πŸ“

μ²˜μŒμ— 더미 λ…Έλ“œ(dummy node)λ₯Ό μΆ”κ°€ν•˜κ³  첫 번째 λ…Έλ“œμ™€ 두 번째 λ…Έλ“œμ˜ 비ꡐλ₯Ό 톡해 포인터λ₯Ό λΆ™μ—¬λ‚˜κ°€λŠ” μ‹μœΌλ‘œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€. 이 과정을 두 리슀트 쀑 ν•˜λ‚˜λΌλ„ 끝에 도달할 λ•ŒκΉŒμ§€ λ°˜λ³΅ν•©λ‹ˆλ‹€.

λ§ˆμ§€λ§‰μœΌλ‘œ 아직 λ‚¨μ•„μžˆλŠ” λ…Έλ“œλ“€μ„ λ³‘ν•©λœ 리슀트의 끝에 μΆ”κ°€ν•˜κ³ , 더미 λ…Έλ“œμ˜ λ‹€μŒ λ…Έλ“œλ₯Ό λ°˜ν™˜ν•˜μ˜€μŠ΅λ‹ˆλ‹€. λ”λ―Έλ…Έλ“œλŠ” λ‹€μŒ 포인터λ₯Ό 뢙이기 μœ„ν•œ 사전 μž‘μ—…μ΄λ‹ˆκΉŒμš”.

Tip

μ½”λ“œλ₯Ό λ³Ό λ•Œ μ•Œλ©΄ 쒋을 점
Swift의 ?? μ—°μ‚°μžλŠ” Nil-Collapsing μ—°μ‚°μžλ‘œ, λ§Œμ•½ μ•žμ— μžˆλŠ” 값이 nil이라면 ??μ—°μ‚°μž λ’€μ˜ κ°’μœΌλ‘œ μ²˜λ¦¬ν•©λ‹ˆλ‹€.

141. Linked List Cycle

Complexities πŸ“ˆ

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

Explanation πŸ“

Floyd's Tortoise and Hareλ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€. Joma Tech의 If Programming Was An Anime을 λ³Έ 게 μƒκ°λ‚˜λŠ” λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€ πŸ˜‚
이 μ•Œκ³ λ¦¬μ¦˜μ€ 두 개의 포인터λ₯Ό ν™œμš©ν•˜κ³ , ν•˜λ‚˜λŠ” ν•˜λ‚˜μ˜ λ…Έλ“œλ₯Ό, ν•˜λ‚˜λŠ” 두 개의 λ…Έλ“œλ₯Ό μ΄λ™ν•˜μ—¬ κ³„μ†ν•΄μ„œ μˆœνšŒν•©λ‹ˆλ‹€. λ§Œμ•½ 두 ν¬μΈν„°μ˜ μœ„μΉ˜κ°€ 동일해진닀면, Cycle이 μžˆλ‹€λŠ” λœ»μž…λ‹ˆλ‹€.

206. Reverse Linked List

Complexities πŸ“ˆ

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

Explanation πŸ“

μž¬κ·€λ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€. κΈ°μ‘΄ ListNode의 연결을 μ•½κ°„μ”© μˆ˜μ •ν•˜μ—¬ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

226. Invert Binary Tree

Complexities πŸ“ˆ

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

Explanation πŸ“

TreeNode의 left와 right을 λ³€κ²½ν•΄μ£Όκ³ ,
각 left와 rightλ₯Ό μž¬κ·€λ‘œ 돌렀 값을 μ²˜λ¦¬ν–ˆμŠ΅λ‹ˆλ‹€.
μ΄λ²ˆμ—λŠ” μž¬κ·€λ‘œ ν’€λ§Œν•œ λ¬Έμ œλ“€μ΄ λ§Žμ•˜λ„€μš” ☺️

@DaleSeo DaleSeo added the week2 label May 4, 2024
@DaleSeo DaleSeo added this to the week2 milestone May 4, 2024
@WhiteHyun WhiteHyun changed the title Week 2 Solution Explanation [Hyun] Week 2 Solution Explanation May 4, 2024
Copy link
Contributor

@SamTheKorean SamTheKorean left a comment

Choose a reason for hiding this comment

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

PR에 정리λ₯Ό λ„ˆλ¬΄ μž˜ν•΄ μ£Όμ…”μ„œ 저도 문제 ν‘ΈλŠ”λ° μ°Έκ³ ν•΄μ„œ 도움이 많이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€! Joma Tech 이뢄 예λŠ₯ μœ νŠœλ²„μΈ 쀄 μ•Œμ•˜λŠ”λ° ν…Œν¬ μœ νŠœλ²„μ˜€κ΅°μš” γ…Žγ…Ž

@DaleSeo
Copy link
Contributor

DaleSeo commented May 8, 2024

ν˜Ήμ‹œ 이 뢀뢄은 μ–΄λ–€ μ˜λ„κ°€ μžˆμœΌμ…”μ„œ μ„€μ •ν•˜μ…¨μ„κΉŒμš”? μš°μ„ μˆœμœ„κ°€ λ†’μœΌλ‹ˆ 빨리 λ¦¬λ·°ν•΄λ‹¬λΌλŠ” μ˜λ―Έμ…¨μ„κΉŒμš”? γ…‹γ…‹

Shot 2024-05-07 at 20 54 48@2x

@DaleSeo
Copy link
Contributor

DaleSeo commented May 8, 2024

κΉƒν—ˆλΈŒμ—μ„œ 이 κΈ°λŠ₯ μ“°μ‹œλŠ” λΆ„ 첨 λ΅ˆμš”! πŸ‘ 저도 써봐야 κ² λ„€μš© γ…Žγ…Ž

Shot 2024-05-07 at 20 56 44@2x

κ·Έλ‚˜μ €λ‚˜ 이 문법은 μžλ°”μŠ€ν¬λ¦½νŠΈμ—λ„ μžˆμ–΄μš” πŸ˜›

Copy link
Contributor

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

덕뢄에 μž¬λ°ŒλŠ” Swfit μ½”λ“œ 잘 μ½μ—ˆμŠ΅λ‹ˆλ‹€! πŸ˜„


final class LeetCode226 {
func invertTree(_ node: TreeNode?) -> TreeNode? {
guard let node else { return nil }
Copy link
Contributor

Choose a reason for hiding this comment

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

Swift 쫌 μ©ŒλŠ”λ“― γ…‹γ…‹

// ν•˜μ§€λ§Œ, 각 κ΄„ν˜Έμ˜ μ•„μŠ€ν‚€ 값이 3 이상 λ‚˜λŠ” κ²½μš°λŠ” μ„œλ‘œ λ§žμ§€ μ•ŠμŒ.
// Swiftμ—μ„œλŠ” asciiValue ν”„λ‘œνΌν‹°μ˜ νƒ€μž…μ΄ `UInt8` 이기 λ•Œλ¬Έμ— μŒμˆ˜κ°€ μ—†μ–΄μ„œ κ°€λŠ₯ν•œ 비ꡐ!
// `&-` λŠ” μ˜€λ²„ν”Œλ‘œμš° μ—°μ‚°μœΌλ‘œ, λ§Œμ•½ μ˜€λ²„ν”Œλ‘œμš°κ°€ λ‚œλ‹€λ©΄ λ¬΄μ‹œν•¨.
if character.asciiValue! &- stack.last!.asciiValue! > 2 {
Copy link
Contributor

Choose a reason for hiding this comment

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

λ¬΄μ‹œλ¬΄μ‹œ ν•©λ‹ˆλ‹€ γ…‹γ…‹

@WhiteHyun
Copy link
Member Author

WhiteHyun commented May 8, 2024

ν˜Ήμ‹œ 이 뢀뢄은 μ–΄λ–€ μ˜λ„κ°€ μžˆμœΌμ…”μ„œ μ„€μ •ν•˜μ…¨μ„κΉŒμš”? μš°μ„ μˆœμœ„κ°€ λ†’μœΌλ‹ˆ 빨리 λ¦¬λ·°ν•΄λ‹¬λΌλŠ” μ˜λ―Έμ…¨μ„κΉŒμš”? γ…‹γ…‹

Shot 2024-05-07 at 20 54 48@2x

μ•„ 이 뢀뢄은 κΆŒν•œμ΄ μžˆμ–΄μ„œ μ œκ°€ λ”°λ‘œ μ„€μ •ν•΄λ‘μ—ˆμŠ΅λ‹ˆλ‹€ ν•˜ν•˜; 큰 μ˜λ―ΈλŠ” 담지 μ•Šμ•˜μ–΄μš”.
κ±΄λ“œλ €μ„œλŠ” μ•ˆλ˜λŠ” κ±Έ μ œκ°€ κ±΄λ“œλ¦° 것 κ°™κ΅°μš”. πŸ₯²

@DaleSeo
Copy link
Contributor

DaleSeo commented May 8, 2024

μ•„ 이 뢀뢄은 κΆŒν•œμ΄ μžˆμ–΄μ„œ μ œκ°€ λ”°λ‘œ μ„€μ •ν•΄λ‘μ—ˆμŠ΅λ‹ˆλ‹€ ν•˜ν•˜; 큰 μ˜λ―ΈλŠ” 담지 μ•Šμ•˜μ–΄μš”.
κ±΄λ“œλ €μ„œλŠ” μ•ˆλ˜λŠ” κ±Έ μ œκ°€ κ±΄λ“œλ¦° 것 κ°™κ΅°μš”. πŸ₯²

γ…Žγ…Ž 별 κ±°λŠ” μ•„λ‹ˆκ³ μš”. Priority 섀정을 ν•˜λ‹ˆκΉŒ ν”„λ‘œμ νŠΈ λ³΄λ“œμ—μ„œ λ„ˆλ¬΄ νŠ€μ–΄λ³΄μ΄λ”λΌκ³ μš”. ν˜Όμ„ μ΄ 없도둝 μ•ˆ μ“°λŠ” ν•„λ“œλŠ” μ‚­μ œν•˜μ˜€μŠ΅λ‹ˆλ‹€. πŸ˜„

@DaleSeo DaleSeo merged commit e01b39c into DaleStudy:main May 8, 2024
1 check passed
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.

3 participants