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 ljedd2 #161

Merged
merged 15 commits into from
Nov 22, 2024
Merged

11 ljedd2 #161

merged 15 commits into from
Nov 22, 2024

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 22, 2024

πŸ”— 문제 링크

λ°±μ€€ μš°μ„ μˆœμœ„ 큐 | BOJ 2075 N번째 큰 수

문제 μ„€λͺ…

NΓ—N의 ν‘œμ— 수 N2개 μ±„μ›Œμ Έ μžˆλ‹€. μ±„μ›Œμ§„ μˆ˜μ—λŠ” ν•œ 가지 νŠΉμ§•μ΄ μžˆλŠ”λ°, λͺ¨λ“  μˆ˜λŠ” μžμ‹ μ˜ ν•œ μΉΈ μœ„μ— μžˆλŠ” μˆ˜λ³΄λ‹€ ν¬λ‹€λŠ” 것이닀. N=5일 λ•Œμ˜ 예λ₯Ό 보자.

image

μ΄λŸ¬ν•œ ν‘œκ°€ μ£Όμ–΄μ‘Œμ„ λ•Œ, N번째 큰 수λ₯Ό μ°ΎλŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

μ œν•œ 쑰건
첫째 쀄에 **N(1 ≀ N ≀ 1,500)**이 주어진닀.
λ‹€μŒ N개의 μ€„μ—λŠ” 각 μ€„λ§ˆλ‹€ N개의 μˆ˜κ°€ 주어진닀.
ν‘œμ— 적힌 μˆ˜λŠ” -10얡보닀 ν¬κ±°λ‚˜ κ°™κ³ , 10얡보닀 μž‘κ±°λ‚˜ 같은 μ •μˆ˜μ΄λ‹€.

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

1μ‹œκ°„

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

μš°μ„ μˆœμœ„ν μœ ν˜• 문제 ν‘ΈλŠ”λ° μž¬λ―Έκ°€ λ“€λ € 도전해본 λ¬Έμ œμž…λ‹ˆλ‹€!
일단 각 쀄에 주어진 수의 λ²”μœ„κ°€ μ–‘ μ˜†μœΌλ‘œ 10얡이기 λ•Œλ¬Έμ— 일반적인 μ ‘κ·ΌμœΌλ‘œλŠ” 어림도 μ—†λ‹€λŠ” 것을 λŠλ‚„ 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ N^2의 μˆ«μžλ“€μ„ λͺ¨λ‘ μ‚΄νŽ΄λ³΄λ©΄μ„œ λ‘œμ§μ„ μˆ˜ν–‰ν•˜λ˜ μ΄λŸ¬ν•œ μž…λ ₯듀을 크기가 N 짜리 μš°μ„ μˆœμœ„ νμ—μ„œ μ²˜λ¦¬ν•΄μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

문제 풀이 κ³Όμ •

  1. μš°μ„ μˆœμœ„ 큐 μ•ˆμ— λ“€μ–΄μžˆλŠ” μ›μ†Œμ˜ κ°œμˆ˜κ°€ N개 미만이라면 μš°μ„ μˆœμœ„ 큐에 μ§‘μ–΄λ„£μŠ΅λ‹ˆλ‹€.
  2. μš°μ„ μˆœμœ„ 큐 μ•ˆμ— λ“€μ–΄μžˆλŠ” μ›μ†Œμ˜ κ°œμˆ˜κ°€ N개라면  Β 
    1. ν˜„μž¬ ν™•μΈν•˜κ³  μžˆλŠ” μˆ«μžκ°€ μš°μ„ μˆœμœ„ 큐의 μ΅œμ†Ÿκ°’λ³΄λ‹€ 크닀면? Β  Β  Β 
      ->Β μš°μ„ μˆœμœ„ 큐의 μ΅œμ†Ÿκ°’μ„ 제거, ν˜„μž¬ ν™•μΈν•˜κ³  μžˆλŠ” 숫자λ₯Ό μš°μ„ μˆœμœ„ 큐에 μ‚½μž…ν•©λ‹ˆλ‹€.

μœ„ 과정을 ν’€μ–΄μ“°λ©΄ μ•„λž˜μ™€ κ°™μŠ΅λ‹ˆλ‹€
image

heappush()λ₯Ό μ΄μš©ν•˜λ©΄, μš°μ„  μˆœμœ„μ— λ”°λ₯Έ 정렬이 μΌμ–΄λ‚˜λ―€λ‘œ μ°ΎλŠ” 값이 맨 μ•žμœΌλ‘œ 올 λ•Œ κΉŒμ§€ 맨 μ•ž 값을 계속 μ œκ±°ν•΄μ£Όκ³ , N번째 큰 값을 좜λ ₯ν•  수 있게 λ©λ‹ˆλ‹€.

μ΅œμ’… μ½”λ“œ

import sys, heapq
input = sys.stdin.readline

n = int(input())
heap = []

for _ in range(n):
    for number in map(int, input().split()):

        if len(heap) < n: # 비ꡐ λŒ€μƒμ΄ λͺ¨μžλž„ 경우 
            heapq.heappush(heap, number) #κ·ΈλŒ€λ‘œ μΆ”κ°€ 

        else:
            if number > heap[0]: # 제일 μž‘μ€κ²ƒλ³΄λ‹€ 크면 
                heapq.heapreplace(heap, number) #μž‘μ€κ±° λΉΌκ³  큰거 λ„£μ–΄μ€Œ 

print(heap[0]) # λ§¨μ•žμ— μžˆλŠ”κ²Œ N번째둜 ν°λ†ˆ 

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

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

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

μš°μ„ μˆœμœ„ 큐 λ§ˆμŠ€ν„°λ‘œ κ°€κ³  κ³„μ‹œκ΅°μš” 🫒
μ €λŠ” μ΅œκ·Όμ— 그리디 μ•Œκ³ λ¦¬μ¦˜ 문제λ₯Ό ν’€λ‹€κ°€ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν–ˆλŠ”λ°, μ²˜μŒμ— μ™„μ „ 잘λͺ» μƒκ°ν•˜κ³  μžˆλ‹€κ°€ μš°μ„ μˆœμœ„ 큐λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€λŠ” κ±Έ μ•Œκ³  어리λ‘₯절 ν–ˆμŠ΅λ‹ˆλ‹Ή...πŸ₯Ή μ–΄λ–€ μƒν™©μ—μ„œ μš°μ„ μˆœμœ„ 큐가 이용되면 쒋을지 μ—°μŠ΅μ„ 많이 해봐야 ν•  것 κ°™μ•„μš”!! 정은 λ‹˜ 덕뢄에 μ‚¬μš©ν•  수 μžˆλŠ” κ³³ ν•˜λ‚˜λ₯Ό 또 ν•™μŠ΅ν–ˆμŠ΅λ‹ˆλ‹€!

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

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

8μ°¨μ‹œ PR λ•Œ 이 문제λ₯Ό ν•œλ²ˆ ν’€μ–΄λ³΄μ•˜λŠ”λ° μ •μ€λ‹˜μ΄ ν‘Έμ…¨λ˜ 둜직과 거의 λ™μΌν•˜λ„€μš”! 저도 μ²˜μŒμ—λŠ” N개의 수λ₯Ό heap에 넣은 뒀에 λ‹€μŒ μˆ˜λΆ€ν„°λŠ” N번째 큰 μˆ˜λ³΄λ‹€ κ·Έ λ‹€μŒ ν–‰μ˜ μˆ«μžκ°€ 더 크닀면, heapμ—λŠ” κ·Έ λ‹€μŒ ν–‰μ˜ 숫자λ₯Ό λŒ€μ‹  λ„£κ³  μ•„λ‹ˆλ©΄ 넣지 μ•ŠλŠ” λ°©μ‹μœΌλ‘œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€πŸ˜Š

이번 PR도 μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€πŸŒ»


else:
if number > heap[0]: # 제일 μž‘μ€κ²ƒλ³΄λ‹€ 크면
heapq.heapreplace(heap, number) #μž‘μ€κ±° λΉΌκ³  큰거 λ„£μ–΄μ€Œ
Copy link
Member

@janghw0126 janghw0126 Sep 26, 2024

Choose a reason for hiding this comment

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

μš°μ™€! 파이썬 heapq λͺ¨λ“ˆ μ€‘μ—μ„œ heapreplace ν•¨μˆ˜λΌλŠ” 것이 μžˆμ—ˆκ΅°μš”!
μ •μ€λ‹˜ 덕뢄에 heapreplace ν•¨μˆ˜κ°€ heapμ—μ„œ μ΅œμ†Œκ°’μ„ μ œκ±°ν•˜κ³  item을 μΆ”κ°€ν•˜λŠ” 역할을 ν•œλ‹€λŠ” 것을 μ•Œμ•„κ°‘λ‹ˆλ‹€πŸ«’
λ‹€μŒμ— 저도 ν•œλ²ˆ μ¨λ³΄κ² μŠ΅λ‹ˆλ‹·!

@LJEDD2 LJEDD2 merged commit a7069d8 into main Nov 22, 2024
1 check passed
@LJEDD2 LJEDD2 deleted the 11-ljedd2_ branch November 22, 2024 01:12
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