Skip to content

[Hyun] Week 4 Solution Explanation #90

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 6 commits into from
May 26, 2024

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented May 24, 2024

49. Group Anagrams

Complexities πŸ“ˆ

  • Time Complexity: $O(n * m log m)$ (m = string length, n = string array's length)
  • Space Complexity: $O(n)$

Explanation πŸ“

  1. 처음 μ ‘κ·Όν•  λ•Œ λ‹¨μˆœνžˆ Set νƒ€μž…μœΌλ‘œ λ¬Άμ–΄ Dictionary의 keyκ°’μœΌλ‘œ μ œκ³΅ν•¨
  2. "good"κ³Ό "god"이 같이 λ¬Άμ΄λŠ” λ¬Έμ œμ μ„ 발견
  3. Set λŒ€μ‹ μ— μ •λ ¬λœ 배열값을 key둜 ν•˜μ—¬ Dictionary에 μΆ”κ°€

190. Reverse Bits

Complexities πŸ“ˆ

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

Explanation πŸ“

  1. μ²˜μŒμ— 두 κ°€μ§€μ˜ μ ‘κ·Ό 방식이 λ– μ˜€λ¦„
    a. 2μ§„μˆ˜λ₯Ό λ¬Έμžμ—΄λ‘œ λ³€ν™˜ β†’ λ¬Έμžμ—΄μ„ 거꾸둜 λ³€ν™˜ β†’ prefixκ°€ 0μ΄μ–΄μ„œ μΆ•μ•½λœ 길이만큼 suffix에 0을 ν™•μž₯ β†’ μ •μˆ˜νƒ€μž…μœΌλ‘œ λ³€ν™˜ 및 리턴
    b. 각 λΉ„νŠΈλ§ˆλ‹€ μ‰¬ν”„νŠΈ μ—°μ‚°(<<, >>)κ³Ό μ•€λ“œ μ—°μ‚°(&)을 ν™œμš©ν•˜μ—¬ λ”ν•˜κ³  리턴
  2. μ „λΆ€ κ΅¬ν˜„ν•˜κ³  μ‹œκ°„μ°¨λ₯Ό ν™•μΈν•΄λ³΄μ•˜μœΌλ‚˜, 32자리수λ₯Ό μ²˜λ¦¬ν•˜λŠ” 것에 λŒ€ν•΄ μ‹œκ°„ 계산은 μœ μ˜λ―Έν•œ 차이λ₯Ό μ°Ύμ§€ λͺ»ν•¨

191. Number of 1 Bits

Complexities πŸ“ˆ

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

Explanation πŸ“

Results
Screenshot 2024-05-24 at 3 51 10β€―PM
  • Swiftμ—λŠ” Intνƒ€μž…μ˜ λ©”μ„œλ“œλ‘œ nonzeroBitCountλΌλŠ” ν”„λ‘œνΌν‹°κ°€ μ‘΄μž¬ν•¨, μ΄λŠ” Swiftμ—μ„œ 자체 μ œκ³΅ν•΄μ£ΌλŠ” ν”„λ‘œνΌν‹°λ‘œ, μ‹€μ œ λΉ„νŠΈ 쀑 1의 총 개수λ₯Ό 리턴함

  • Swiftμ—μ„œ μ œκ³΅ν•΄μ£ΌλŠ” 것을 μ‚¬μš©ν•˜μ§€ μ•Šμ„ κ²½μš°λ„ κ΅¬ν˜„ν•˜μ˜€κ³ , 비ꡐ κ²°κ³Ό μœ μ˜λ―Έν•œ μ°¨μ΄λŠ” μ‘΄μž¬ν•˜μ§€ μ•Šμ•˜μŒ

  • Test code

268. Missing Number

Complexities πŸ“ˆ

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

Explanation πŸ“

Results
Screenshot 2024-05-24 at 4 09 41β€―PM

[0 ... n]μ—μ„œ μƒλž΅λœ 숫자λ₯Ό ꡬ해야 함

  1. μ„Έ κ°€μ§€μ˜ 방법이 λ– μ˜€λ¦„

    1. XOR둜 풀이
    2. [0 ... n]의 총합과 μ£Όμ–΄μ§„ 숫자 λ°°μ—΄μ˜ 총합을 λΉΌλŠ” 풀이
    3. Set둜 λ¬Άμ–΄μ„œ μ°¨μ§‘ν•© μ—°μ‚°
  2. μ „λΆ€ κ΅¬ν˜„ ν›„ μ‹œκ°„ 비ꡐ κ²°κ³Ό, 짧은 풀이이기 λ•Œλ¬Έμ— μ‹€ν–‰ ν™˜κ²½μ— 따라 속도가 λ‹¬λΌμ‘ŒμŒ

338. Counting Bits

Complexities πŸ“ˆ

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

Explanation πŸ“

Results
image
  1. μ²˜μŒμ—λŠ” Swiftμ—μ„œ μžμ²΄μ œκ³΅ν•΄μ£ΌλŠ” nonzeroBitCount ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜μ—¬ 풀이함

  2. ν•˜μ§€λ§Œ dp둜 풀이할 수 μžˆλ‹€λŠ” 사싀을 κΉ¨λ‹«κ³  μƒˆλ‘­κ²Œ 풀이함

    1. 6(110)은 3(11)의 1의 개수 + 6(110)의 μ΅œν•˜μœ„ λΉ„νŠΈ,
    2. 3(11)은 1(1)의 1의 개수 + 3(11)의 μ΅œν•˜μœ„ λΉ„νŠΈ,
    3. ...
    • 즉 점화식은 μ•„λž˜μ™€ κ°™μŒ

$$ a_n= \begin{cases} a_\frac{n}{2} + (n \land 1) \text{ for } n \geq 2 \\ a_1=1 \\ a_0=0 \end{cases} $$

  1. LeetCodeμ—μ„œ μ£Όμ–΄μ§„ ν…ŒμŠ€νŠΈμΌ€μ΄μŠ€λ‘œλŠ” μ‹œκ°„ 계산이 μ–΄λ ΅λ‹€κ³  νŒλ‹¨ν•˜μ—¬ 10_000_000의 값을 μ£Όκ³  풀이 속도 μΈ‘μ •, dp 풀이가 ν™•μ‹€νžˆ 빨랐음(μ–΄μ°Œλ³΄λ©΄ λ‹Ήμ—°ν•œ 거일 μˆ˜λ„....)

@WhiteHyun WhiteHyun self-assigned this May 24, 2024
@leokim0922
Copy link
Contributor

μŠ€μœ„ν”„νŠΈλ₯Ό λͺ°λΌμ„œ μ •ν™•νžˆ μ΄ν•΄λŠ” μ•ˆκ°€μ§€λ§Œ λ©‹μ§„ λΆ„μ„μž…λ‹ˆλ‹€ γ…Žγ…Ž

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덕뢄에 많이 λ°°μ› μŠ΅λ‹ˆλ‹€. κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!

@SamTheKorean SamTheKorean merged commit dc33916 into DaleStudy:main May 26, 2024
1 check passed
@WhiteHyun WhiteHyun deleted the whitehyun/week4 branch May 28, 2024 03:03
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