Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 다시
- 트리
- 수학
- 에라토스테네스의 체
- 재귀
- 서브쿼리
- 크루스칼
- DP
- 자료구조
- BFS
- 우선순위큐
- join
- 다이나믹 프로그래밍
- MST
- 브루트포스
- 분할정복
- 백트래킹
- 누적합
- 그래프 탐색
- 구현
- 시뮬레이션
- DFS
- 그래프 이론
- 해시
- 투포인터
- 다이나믹프로그래밍
- 플로이드-워셜
- 그리디
- 다익스트라
- GROUP BY
Archives
- Today
- Total
기록하고 까먹지 말기
7662 본문
날짜 : 2023. 10. 10
사용 언어 : python
문제
코드
import sys
import heapq
INF = int(1e20)
def solution(operations):
maxheap = list()
minheap = list()
hash = dict()
cnt = 0 # 전체 큐에 남아있는 원소의 개수
for temp in operations:
op, num = temp.split()
num = int(num)
if op == "I": # 삽입
cnt += 1
heapq.heappush(minheap, num)
heapq.heappush(maxheap, num * (-1))
if num not in hash: hash[num] = 1
else: hash[num] += 1
continue
if cnt == 0: continue # 더이상 뺄 수 없는 상태 => 추출 연산 패스
cnt -= 1
if num == -1: # 최소값 추출
while True:
n = heapq.heappop(minheap)
if hash[n] > 0:
hash[n] -= 1 # 뺄 수 있는 값인지 체크
break
else: # 최대값 추출
while True:
n = heapq.heappop(maxheap) * (-1)
if hash[n] > 0:
hash[n] -= 1
break
if cnt == 0: return [INF, INF] # 뺄 원소가 없는 경우
min_res, max_res = INF, INF * (-1)
for i in list(hash.keys()):
if hash[i] > 0: # 최대 / 최소값 갱신
min_res = min(min_res, i)
max_res = max(max_res, i)
return [max_res, min_res]
t = int(sys.stdin.readline())
for _ in range(t):
operation = list()
n = int(sys.stdin.readline())
for _ in range(n):
temp = sys.stdin.readline().rstrip()
operation.append(temp)
a, b = solution(operation)
if a != INF and b != INF:
print(a, b)
else:
print("EMPTY")
풀이
- 프로그래머스의 이중우선순위큐와 동일한 문제다. ( https://school.programmers.co.kr/learn/courses/30/lessons/42628 )
- 풀이는 동일하게 hash, maxheap, minheap을 구현하여 풀이한다. (https://yhah.tistory.com/424)
- 다만 원소의 범위가 -2^32에서 2^32이기 때문에 적당히 큰 수를 지정해야 해당 문제에서 오답이 발생하지 않는다.
알게된 점
- int(1e9)로 지정해서 풀었더니 93%에서 오답이 발생했었다.
참고 사이트
-