Skip to content

[Hyun] Week 11 Solution Explanation #182

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 5 commits into from
Jul 15, 2024

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented Jul 13, 2024

647. Palindromic Substrings

Complexities πŸ“ˆ

  • Time Complexity: $O(N^2)$
  • Space Complexity: $O(1)$

Explanation πŸ“

Screenshot 2024-07-14 at 2 54 17β€―AM

μ²˜μŒμ—λŠ” Brute Force둜 풀이( $O(N^3)$ )ν–ˆμœΌλ‚˜ κ½€λ‚˜ μ‹œκ°„μ΄ 많이 μ†Œλͺ¨λ˜λŠ” 것을 ν™•μΈν•˜μ˜€κ³ , 더 λ‚˜μ€ λ°©μ•ˆμ„ κ³ λ €ν•΄λ³΄κΈ°λ‘œ ν–ˆμŠ΅λ‹ˆλ‹€.
이전 문제 5. Longest Palindromic Substringμ—μ„œ νŒ°λ¦°λ“œλ‘¬μ΄ 될 λ¬Έμžμ—΄μ˜ μ€‘κ°„μ—μ„œ μ‹œμž‘ν•˜μ—¬ μ–‘μ˜†μœΌλ‘œ λ»—μ–΄λ‚˜κ°€λŠ” 풀이법에 μ°©μ•ˆν•˜μ—¬ $O(N^2)$ 으둜 풀이할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

91. Decode Ways

Complexities πŸ“ˆ

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

Explanation πŸ“

이번 λ¬Έμ œλŠ” 풀이법을 λ„μΆœν•˜λŠ” 게 μ–΄λ €μ› λ„€μš”.
Memoization을 μ΄μš©ν•œ 풀이, 그리고 dpλ₯Ό ν™œμš©ν•œ 풀이 두 κ°€μ§€ λ°©μ‹μœΌλ‘œ μ‹œλ„ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

322. Coin Change

Complexities πŸ“ˆ

  • Time Complexity: $O(N * Amount)$
  • Space Complexity: $O(Amount)$

Explanation πŸ“

λ‹¨μˆœ 그리디 λ¬Έμ œμΈμ€„ μ•Œκ³  ν’€μ—ˆμœΌλ‚˜ [1, 5, 7]이고 amountκ°€ 25인 경우 제일 적은 λ™μ „μ˜ μˆ˜λŠ” κ°’μ–΄μΉ˜κ°€ 5인 동전을 5번 μ‚¬μš©ν•˜λŠ” 것이기 λ•Œλ¬Έμ—, 제일 큰 κ°’ μœ„μ£Όλ‘œ μ²˜λ¦¬ν•΄μ„œλŠ” μ•ˆ λœλ‹€λŠ” κ±Έ κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€.
λ”°λΌμ„œ 제일 적은 개수λ₯Ό κ΅¬ν•˜κ³ μž DPλ₯Ό ν™œμš©ν–ˆμŠ΅λ‹ˆλ‹€. λ™μ „μ˜ κ°’μ–΄μΉ˜λ₯Ό coin으둜 ν•˜κ³  νŠΉμ • κΈˆμ•‘μ— ν•œν•˜μ—¬ 점화식을 λ‹€μŒκ³Ό 같이 $F(n) = MIN(F(n), F(n - coin) + 1)$ 둜 μ„Έμ›Œμ„œ ν’€μ΄ν–ˆμŠ΅λ‹ˆλ‹€.

152. Maximum Product Subarray

Complexities πŸ“ˆ

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

Explanation πŸ“

μ΅œλŒ“κ°’μ„ κ°–κΈ° μœ„ν•΄μ„œλŠ” 각 μΈλ±μŠ€μ— λŒ€μ‘λ˜λŠ” μˆ«μžλ§ˆλ‹€ λˆ„μ μ΅œμ†Ÿκ°’, λˆ„μ μ΅œλŒ“κ°’κ³Ό κ³±ν•œ κ°’κ³Ό μΈλ±μŠ€μ— λŒ€μ‘λ˜λŠ” 숫자 κ·Έ μƒνƒœμ™€λ„ λΉ„κ΅ν•˜λ©° μ΅œλŒ“κ°’μ„ κ΅¬ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

139. Word Break

Complexities πŸ“ˆ

  • Time Complexity: $O(2^N)$
  • Space Complexity: $O(N)$

Explanation πŸ“

μ΅œμ•…μ˜ 경우 λͺ¨λ“  λΆ€λΆ„ λ¬Έμžμ—΄μ„ 탐색할 μˆ˜λ„ μžˆμœΌλ―€λ‘œ μ‹œκ°„λ³΅μž‘λ„λŠ” $O(2^N)$, μž¬κ·€ 호좜 μŠ€νƒμ— 따라 μ΅œλŒ€ κΉŠμ΄κ°€ μ •ν•΄μ§€λ―€λ‘œ κ³΅κ°„λ³΅μž‘λ„λŠ” $O(N)$μž…λ‹ˆλ‹€.

@WhiteHyun WhiteHyun self-assigned this Jul 13, 2024
@WhiteHyun WhiteHyun requested a review from yolophg July 13, 2024 20:24
@WhiteHyun WhiteHyun marked this pull request as ready for review July 13, 2024 23:03
Copy link
Contributor

@yolophg yolophg left a comment

Choose a reason for hiding this comment

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

μ‹œκ°„, 곡간 λ³΅μž‘λ„μ— λŒ€ν•΄ 적어주신 풀이와 ν•¨κ»˜ ν₯미둭게 잘 λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ :)
μ΄λ²ˆμ£Όλ„ 수고 λ§ŽμœΌμ…¨μŠ΅λ‹ˆλ‹€πŸ‘

Comment on lines +15 to +19
for coin in coins {
for index in stride(from: coin, to: dp.count, by: 1) where dp[index - coin] != .max && dp[index - coin] + 1 < dp[index] {
dp[index] = dp[index - coin] + 1
}
}
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 +29 to +46
func numDecodingsUsingMemoization(_ s: String) -> Int {
let messages = s.compactMap { Int(String($0)) }
var cache: [Int: Int] = [s.count: 1]

func dfs(_ index: Int) -> Int {
if let cached = cache[index] {
return cached
}

if messages[index] == 0 {
cache[index] = 0
} else if index + 1 < s.count && messages[index] * 10 + messages[index + 1] < 27 {
cache[index] = dfs(index + 1) + dfs(index + 2)
} else {
cache[index] = dfs(index + 1)
}
return cache[index]!
}
Copy link
Contributor

Choose a reason for hiding this comment

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

cacheλ₯Ό μ‚¬μš©ν•΄ 이미 κ³„μ‚°λœ 값을 μ €μž₯ν•˜κ³  μž¬μ‚¬μš©ν•˜λŠ” λ©”λͺ¨μ΄μ œμ΄μ…˜μ„ ν™œμš©ν•œ 방식 ν₯미둭게 λ΄€μŠ΅λ‹ˆλ‹€πŸ‘

Comment on lines +25 to +29
if negativeCount > 1 {
minNumber = min(current, tempMin * current, tempMax * current)
} else {
minNumber = 0
}
Copy link
Contributor

Choose a reason for hiding this comment

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

음수 κ³± 처리λ₯Ό μƒκ°ν•˜λ©΄, minNumberλŠ” 사싀상 항상 μ—…λ°μ΄νŠΈ λ˜μ–΄μ•Όν•˜λŠ”λ°, 쑰건문을 ν•¨κ»˜ μž‘μ„±ν•˜μ‹  μ΄μœ κ°€ μžˆμœΌμ‹€κΉŒμš”?
별닀λ₯Έ μ΄μœ κ°€ μžˆμœΌμ‹ κ²Œ μ•„λ‹ˆλΌλ©΄, λΆˆν•„μš”ν•œ 쑰건문은 κ°„μ†Œν™”ν•΄λ„ 쒋을 것 κ°™λ‹€λŠ” μ˜κ²¬μž…λ‹ˆλ‹€!

Copy link
Member Author

Choose a reason for hiding this comment

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

μΆ”κ°€μ„€λͺ…을 μ œκ°€ λ„£μ§€ μ•Šμ•˜λ„€μš” γ…Žγ…Ž.. LeetCode의 190번째 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ—μ„œ SwiftλŠ” μ΄μƒν•˜κ²Œ overflow 였λ₯˜κ°€ λ‚˜νƒ€λ‚˜λ”λΌκ³ μš”. κ·Έλž˜μ„œ 190번째 였λ₯˜λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ €λŸ° νŠΈλ¦­μ„ μ΄μš©ν•΄ λ³΄μ•˜μŠ΅λ‹ˆλ‹€. πŸ₯Ή

Comment on lines +11 to +40
// dp way
func countSubstringsUsingDP(_ s: String) -> Int {
let array = Array(s)
let n = array.count
var count = 0

var dp: [[Bool]] = .init(
repeating: .init(repeating: false, count: n),
count: n
)

for i in dp.indices {
dp[i][i] = true
count += 1
}

for i in dp.indices.dropLast() where array[i] == array[i + 1] {
dp[i][i + 1] = true
count += 1
}

for length in stride(from: 3, through: n, by: 1) {
for i in 0 ... n - length where array[i] == array[i + length - 1] && dp[i + 1][i + length - 2] {
dp[i][i + length - 1] = true
count += 1
}
}

return count
}
Copy link
Contributor

Choose a reason for hiding this comment

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

πŸ‘

@WhiteHyun WhiteHyun merged commit 1c0b471 into DaleStudy:main Jul 15, 2024
@WhiteHyun WhiteHyun deleted the whitehyun/week11 branch July 15, 2024 05:51
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.

2 participants