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

11-pu2rile #37

Merged
merged 3 commits into from
Aug 5, 2024
Merged

11-pu2rile #37

merged 3 commits into from
Aug 5, 2024

Conversation

pu2rile
Copy link
Collaborator

@pu2rile pu2rile commented Jul 13, 2024

πŸ”— 문제 링크

κ°•μ˜μ‹€

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

40λΆ„

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

문제 μš”μ•½

  • κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ΄ μ£Όμ–΄μ‘Œμ„ λ•Œ λ™μ‹œμ— μ—¬λŸ¬ κ°•μ˜λ₯Ό 진행할 수 μžˆλŠ” μ΅œμ†Œν•œμ˜ κ°•μ˜μ‹€ 수 κ΅¬ν•˜κΈ°
  • 각 κ°•μ˜λŠ” κ²ΉμΉ˜μ§€ μ•Šλ„λ‘ κ°•μ˜μ‹€ λ°°μ •

μš°μ„ μˆœμœ„ 큐

  • 일반적인 큐와 달리 각 μš”μ†Œκ°€ μš°μ„ μˆœμœ„λ₯Ό 가지고 있으며, μš°μ„ μˆœμœ„κ°€ 높은 μš”μ†Œκ°€ λ¨Όμ € λ‚˜μ˜€λŠ” 자료ꡬ쑰
  • μ‚½μž…κ³Ό μ‚­μ œ μ—°μ‚°μ—μ„œ νš¨μœ¨μ„±μ„ 보μž₯ν•˜λ©° νž™(Heap) 자료 ꡬ쑰λ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬ν˜„

νŒŒμ΄μ¬μ—μ„œμ˜ μš°μ„ μˆœμœ„ 큐

νŒŒμ΄μ¬μ—μ„œλŠ” heapq λͺ¨λ“ˆμ„ 톡해 μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©
heapq λŠ” μ΅œμ†Œ νž™μ„ 기본으둜 ν•˜λ©° μš”μ†Œ μ‚½μž…κ³Ό μ‚­μ œλ₯Ό 효율적으둜 처리 κ°€λŠ₯

  • μ‚½μž… : heapq.heappush(heap, item) νž™μ— μƒˆλ‘œμš΄ μš”μ†Œ μΆ”κ°€
  • μ‚­μ œ : heapq.heappop(heap) νž™μ—μ„œ κ°€μž₯ μž‘μ€ μš”μ†Œ 제거 ν›„ λ°˜ν™˜

λ¬Έμ œμ— 적용

  1. λͺ¨λ“  κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ μ΅œμ†Œ νž™μ— μ €μž₯ν•΄μ„œ μ‹œμž‘ μ‹œκ°„μ΄ λΉ λ₯Έ μˆœμ„œλŒ€λ‘œ μ •λ ¬
  2. ν˜„μž¬ μ‚¬μš© 쀑인 κ°•μ˜μ‹€μ˜ μ’…λ£Œ μ‹œκ°„μ„ μ΅œμ†Œ νž™μ— μ €μž₯ν•΄μ„œ κ°€μž₯ 빨리 λλ‚˜λŠ” κ°•μ˜μ‹€ μ°ΎκΈ°
  3. μƒˆλ‘œμš΄ κ°•μ˜κ°€ μ‹œμž‘λ  λ•Œ κ°€μž₯ 빨리 λλ‚˜λŠ” κ°•μ˜μ‹€μ˜ μ’…λ£Œ μ‹œκ°„μ΄ ν˜„μž¬ κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μœΌλ©΄ ν•΄λ‹Ή κ°•μ˜μ‹€μ„ μž¬μ‚¬μš©
  4. λ§Žμ€ κ°•μ˜ μ‹œμž‘ μ‹œκ°„μ™€ μ’…λ£Œ μ‹œκ°„μ„ μ²˜λ¦¬ν•˜λ©΄μ„œ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(log n)

μ•Œκ³ λ¦¬μ¦˜

  1. κ°•μ˜ 정보 μž…λ ₯λ°›κΈ°
  2. λͺ¨λ“  κ°•μ˜ 정보λ₯Ό 졜고 νž™μ— μ €μž₯
  3. 첫 번째 κ°•μ˜λ₯Ό μ΅œμ†Œ νž™μ—μ„œ κΊΌλ‚΄κ³  ν•΄λ‹Ή κ°•μ˜μ˜ μ’…λ£Œ μ‹œκ°„μ„ ν˜„μž¬ κ°•μ˜μ‹€ νž™μ— μ €μž₯
  4. 반볡
    • κ°•μ˜λ₯Ό μ΅œμ†Œ νž™μ—μ„œ 꺼냄
    • κ°€μž₯ 빨리 λλ‚˜λŠ” κ°•μ˜μ‹€μ˜ μ’…λ£Œ μ‹œκ°„μ΄ ν˜„μž¬ κ°•μ˜ μ‹œμž‘ μ‹œκ°„λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μœΌλ©΄ ν•΄λ‹Ή κ°•μ˜μ‹€ μž¬μ‚¬μš©
    • ν˜„μž¬ κ°•μ˜ 쑰료 μ‹œκ°„μ„ ν˜„μž¬ κ°•μ˜μ‹€ νž™μ— μΆ”κ°€
  5. ν˜„μž¬ κ°•μ˜μ‹€ νž™μ˜ 크기 좜λ ₯

μ½”λ“œ

import heapq
import sys

n = int(sys.stdin.readline())

heap = []   # λͺ¨λ“  κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ μ €μž₯ν•  μ΅œμ†Œ νž™
q = []  # ν˜„μž¬ μ‚¬μš© 쀑인 κ°•μ˜μ‹€μ˜ μ’…λ£Œ μ‹œκ°„μ„ μ €μž₯ν•  μ΅œμ†Œ νž™

# 주어진 κ°•μ˜ 수만큼 λ°˜λ³΅ν•˜λ©΄μ„œ κ°•μ˜ 정보λ₯Ό μž…λ ₯λ°›μ•„ μ΅œμ†Œ νž™μ— μ €μž₯
for _ in range(n):
    num, start, end = map(int, sys.stdin.readline().split())
    heapq.heappush(heap, [start, end, num])

# 첫 번째 κ°•μ˜λ₯Ό μ΅œμ†Œ νž™μ—μ„œ κΊΌλ‚΄μ„œ ν•΄λ‹Ή κ°•μ˜μ˜ μ’…λ£Œ μ‹œκ°„μ„ λ‹€λ₯Έ νž™μ— μ €μž₯
start, end, num = heapq.heappop(heap)
heapq.heappush(q, end)

while heap:
    start, end, num = heapq.heappop(heap)
    # κ°€μž₯ 빨리 λλ‚˜λŠ” κ°•μ˜μ‹€μ˜ μ’…λ£Œ μ‹œκ°„μ΄ ν˜„μž¬ κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„λ³΄λ‹€ μž‘κ±°λ‚˜ κ°™μœΌλ©΄ μ’…λ£Œ μ‹œκ°„μ„ μ΅œμ†Œ νž™μ—μ„œ 제거
    if q[0] <= start:
        heapq.heappop(q)
    heapq.heappush(q, end)  # ν˜„μž¬ κ°•μ˜μ˜ μ’…λ£Œ μ‹œκ°„μ„ μ΅œμ†Œ νž™μ— μΆ”κ°€

print(len(q))   # μ΅œμ†Œ νž™ q의 크기가 ν•„μš”ν•œ κ°•μ˜μ‹€μ˜ 수

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

Copy link
Collaborator

@suhyun113 suhyun113 left a comment

Choose a reason for hiding this comment

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

였,, 저도 heap 자료 ꡬ쑰λ₯Ό μ €λ²ˆμ— κ³΅λΆ€ν–ˆμ—ˆλŠ”λ° λ°˜κ°‘λ„€μš”!! λ¨Όκ°€ λ³΅μŠ΅λ©λ‹ˆλ‹€γ…Žγ…Ž
κ°•μ˜μ˜ μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ μ΅œμ†Œν•œμ˜ κ°•μ˜μ‹€ 수λ₯Ό κ³„μ‚°ν•˜λŠ” 문제? μ‹œκ°„μ΄ κ²ΉμΉ˜μ§€ μ•Šλ„λ‘ ν•΄μ•Ό ν•˜λ‹€ λ³΄λ‹ˆ μ‹œκ°„ν‘œ μ§œλŠ” 기뢄이 λ“œλŠ”?!?!
두 개의 νž™(heap, q)을 μ‚¬μš©ν•΄ κ°•μ˜ μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ κ΄€λ¦¬ν–ˆλŠ”λ°, 문제λ₯Ό μ’€ 더 효율적으둜 ν•΄κ²°ν•˜λŠ” 쒋은 λ°©λ²•μœΌλ‘œ νž™μ„ 잘 μ‚¬μš©ν•˜μ‹  것 κ°™μ•„μš”! PR μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€γ…!!

Copy link
Member

@oesnuj oesnuj left a comment

Choose a reason for hiding this comment

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

νž™μ€ μ‚½μž…, μ‚­μ œμ— μ‹œκ°„ λ³΅μž‘λ„ O(log n)λ₯Ό 가지죠! νž™μ„ ν™œμš©ν•΄μ„œ κ΅¬ν˜„ν•˜λ‹ˆ κ°•μ˜ μ‹œμž‘κ³Ό μ’…λ£Œ μ‹œκ°„μ— λŒ€ν•œ μ΅œμ†Ÿκ°’μ„ λΉ λ₯΄κ²Œ μ°Ύμ•„λ‚Ό 수 μžˆλ„€μš”.
저도 ν’€μ–΄λ΄€λŠ”λ° μ–΄λ–»κ²Œ λ°°μ •ν•΄μ•Ό νš¨μœ¨μ μΌμ§€ 고민을 많이 ν–ˆλ˜ λ¬Έμ œλ„€μš”.
이번 λ¬Έμ œλ„ κ·Έλ ‡κ³  μ§€κΈˆκΉŒμ§€ ν•΄κ²°ν•˜μ‹  λ¬Έμ œλ„ λ³΄λ‹ˆ λ„ν˜„λ‹˜μ€ 그리디 μ•Œκ³ λ¦¬μ¦˜μ„ μ’‹μ•„ν•˜μ‹œλŠ” 것 같기도..?πŸ€”

파이썬이 μ΅μˆ™ν•˜μ§€ μ•Šμ€λ° μ£Όμ„κΉŒμ§€ μžˆμ–΄ μ½”λ“œ 이해가 μ‰¬μ› μ–΄μš”.
이번 PR μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€πŸ˜„

@pu2rile pu2rile merged commit b1afacd into main Aug 5, 2024
@pu2rile pu2rile deleted the 11-pu2rile branch August 5, 2024 06:18
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.

3 participants