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

05 우선순위큐 #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 14235.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import heapq
import sys
input = sys.stdin.readline

n = int(input())

heap = []

for _ in range(n):
a = list(map(int, input().split()))

if a[0] == 0:
if len(heap) > 0:
print(-heap[0])
heapq.heappop(heap)

else:
print(-1)

else:
for i in range(1, len(a)):
heapq.heappush(heap, -a[i])
27 changes: 27 additions & 0 deletions 1655.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import heapq
import sys
input = sys.stdin.readline

n = int(input())

lheap = []
rheap = []

for i in range(n):
num = int(input())

if i % 2 == 0:
heapq.heappush(lheap, -num)

else:
heapq.heappush(rheap, num)

if i > 0:
if -lheap[0] > rheap[0]:
l = heapq.heappop(lheap)
r = heapq.heappop(rheap)

heapq.heappush(lheap, -r)
heapq.heappush(rheap, -l)

print("result: ", -lheap[0])
18 changes: 18 additions & 0 deletions 2075.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import heapq
import sys
input = sys.stdin.readline

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

for _ in range(N):
num = list(map(int, input().split()))
for n in num:
if len(heap) < N:
heapq.heappush(heap, n)
else:
if heap[0] < n:
heapq.heappop(heap)
heapq.heappush(heap, n)

print(heap[0])
34 changes: 34 additions & 0 deletions 2607.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys
input = sys.stdin.readline

N = int(input())
result = 0

word = input()
alpa = [0]*26

for i in range(len(word)-1):
alpa[ord(word[i])-ord('A')] += 1

for _ in range(N-1):
ow = input()
oa = [0]*26
cnt = 0

for i in range(len(ow)-1):
oa[ord(ow[i])-ord('A')] += 1
Comment on lines +18 to +19
Copy link

Choose a reason for hiding this comment

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

알파벳 개수를 따로 저장하여 해결해 주셨네요!👍👍


for i in range(26):
if alpa[i] != oa[i]:
cnt += abs(alpa[i] - oa[i])


if len(word) == len(ow):
if cnt == 0 or cnt == 2:
result +=1

else:
if cnt == 1 and abs(len(word) - len(ow)) == 1:
result += 1
Comment on lines +26 to +32
Copy link

Choose a reason for hiding this comment

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

문제의 핵심을 정확히 파악하셨습니다! 조건 나누기가 아주 깔끔합니다.😮


print(result)
27 changes: 27 additions & 0 deletions programmers_disk_controller
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import heapq
def solution(jobs):
answer = 0 # 작업하는데 걸린 총 소요시간
now = 0 # 현재 시간
i = 0 # 처리한 작업의 수
start = -1 # 직전에 완료한 작업의 시작 시간
heap = [] # 남은 작업 중 현재 처리 가능한 작업을 저장하는 힙

while i < len(jobs):
# 현재 시점에서 처리할 수 있는 작업을 힙에 저장
for j in jobs:
if start < j[0] <= now: # 작업의 요청시간이 직전에 끝낸 작업보다 크고, 현재 시간보다 작은 경우 작업이 가능힘
heapq.heappush(heap, [j[1], j[0]]) # 작업 처리 시간, 작업 요청 시간 순으로 힙에 저장

if len(heap) > 0: # 힙의 길이가 0보다 큰 경우, 힙에 처리할 작업이 남은 경우
current = heapq.heappop(heap) #current는 현재 처리 중인 작업의 정보
start = now
now += current[0]
answer += (now - current[1]) # 작업 요청 시간부터 작업 처리 완료 시간까지 총 소요시간
i += 1
else: # 힙의 길이가 0인 경우, 처리할 작업이 없는 경우
now += 1

answer = answer // len(jobs) # 평균 처리 시간 계산

return answer