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

15-seongwon030 #61

Merged
merged 1 commit into from
Jun 30, 2024
Merged

15-seongwon030 #61

merged 1 commit into from
Jun 30, 2024

Conversation

seongwon030
Copy link
Collaborator

@seongwon030 seongwon030 commented Jun 1, 2024

πŸ”— 문제 링크

쀑앙값 κ΅¬ν•˜κΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„

✨ μˆ˜λ„ μ½”λ“œ

κ΅ν™©ν˜•λ‹˜μ΄ μΆ”μ²œν•΄μ£Όμ‹  문제 λ°”λ‘œ κΏ€κΊ½ν–ˆμŠ΅λ‹ˆλ‹€.
μ—¬κΈ°μ—μ„œ μ“΄ min_heap, max_heap을 μ΄μš©ν–ˆμŠ΅λ‹ˆλ‹€.

쀑앙값 κ΅¬ν•˜λŠ” ν•¨μˆ˜

def push_num(num):
  if len(max_heap) == len(min_heap):
    heapq.heappush(max_heap, -num)
  else:
    heapq.heappush(min_heap, num)

  if min_heap and -max_heap[0] > min_heap[0]:
    min = heapq.heappop(min_heap)
    max = -heapq.heappop(max_heap)
    heapq.heappush(max_heap, -min)
    heapq.heappush(min_heap, max)
  1. ν™€μˆ˜μΌλ•Œλ§Œ 쀑앙값은 짝수일 λ•Œ n//2, ν™€μˆ˜μΌ λ•Œ n//2+1
  2. 쀑앙값인 -max_heap[0]을 인덱슀 kκ°€ 짝수일 λ•Œ (κ°œμˆ˜κ°€ ν™€μˆ˜κ°œμΌ λ•Œ) 리슀트 re에 λ„£λŠ”λ‹€.
  3. λ§ˆμ§€λ§‰μ— n이 짝수면 λ˜ν•œ -max_heap[0]λ₯Ό re에 λ„£λŠ”λ‹€.
  4. reλ₯Ό λŒλ©΄μ„œ cntλ₯Ό 1μ”© λ”ν•œλ‹€.
  5. 10으둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€κ°€ 0일 λ•Œ μ€„λ°”κΏˆμ„ ν•΄ μ€€λ‹€.

@9kyo-hwang
Copy link

μ•ˆλ…•ν•˜μ„Έμš” κ΅¬μ„ μš° μž…λ‹ˆλ‹€

@seongwon030
Copy link
Collaborator Author

μ•ˆλ…•ν•˜μ„Έμš” κ΅¬μ„ μš° μž…λ‹ˆλ‹€

μ•„ κ΅ν™©ν–‰λ‹˜μ΄μ—ˆλ„€

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

μ•„λ‹ˆ 이거 λ„ˆλ¬΄ λ‚ λ¨Ή μ•„λ‹Œκ°€μš” γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹

@yuyu0830 yuyu0830 self-requested a review June 4, 2024 12:52
Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

μΆ”κ°€λœ 것은 Testcase 만큼 λ°˜λ³΅ν•˜λŠ” 뢀뢄이 λŠ˜μ—ˆκ΅°μš”..

@InSange InSange removed the request for review from dhlee777 June 28, 2024 02:40
Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

μ•ˆλ…•ν•˜μ„Έμš” μž₯κ΅ν™©μž…λ‹ˆλ‹€.

@seongwon030 seongwon030 merged commit c0dd36d into main Jun 30, 2024
1 check passed
@seongwon030 seongwon030 deleted the 15-seongwon030 branch June 30, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants