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

16-janghw0126 #183

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
57 changes: 57 additions & 0 deletions janghw0126/BFS/적둝색약.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import sys
from collections import deque

input = sys.stdin.readline

def bfs(x, y, graph, visited):
q = deque()
q.append((x, y))
visited[x][y] = True

# μƒν•˜μ’Œμš° λ°©ν–₯ μ„ μ–Έν•˜κΈ°
dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]

while q:
x, y = q.popleft()

for i in range(4):
nx, ny = x + dx[i], y + dy[i]

if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]:
# 색상이 κ°™κ³  λ°©λ¬Έν•˜μ§€ μ•Šμ€ κ²½μš°μ—λ§Œ 큐에 λ„£μŒ
if graph[x][y] == graph[nx][ny]:
visited[nx][ny] = True
q.append((nx, ny))

n = int(input())
graph = [list(input().strip()) for _ in range(n)]

# 첫 번째 λ°©λ¬Έ 배열은 정상인 (R, G, B ꡬ뢄함)
visited = [[False] * n for _ in range(n)]
normal_count = 0

# 두 번째 λ°©λ¬Έ 배열은 적둝색맹인 (R, G ꡬ뢄 μ•ˆ 함)
visited_rg = [[False] * n for _ in range(n)]
rg_count = 0

# 정상인 κ·Έλ£Ή 개수 κ΅¬ν•˜κΈ°
for i in range(n):
for j in range(n):
if not visited[i][j]:
bfs(i, j, graph, visited)
normal_count += 1

# 적둝색맹인 경우 'G'λ₯Ό 'R'둜 λ°”κΎΈκΈ°
for i in range(n):
for j in range(n):
if graph[i][j] == 'G':
graph[i][j] = 'R'

# 적둝색맹인 κ·Έλ£Ή 개수 κ΅¬ν•˜κΈ°
for i in range(n):
for j in range(n):
if not visited_rg[i][j]:
bfs(i, j, graph, visited_rg)
rg_count += 1

print(normal_count, rg_count)
40 changes: 40 additions & 0 deletions janghw0126/BFS/ν† λ§ˆν† .py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from collections import deque
import sys
input = sys.stdin.readline

# ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό μž…λ ₯λ°›μŒ
m, n = map(int, input().split())
tomato_grid = [list(map(int, input().split())) for _ in range(n)]

# 읡은 ν† λ§ˆν†  μ’Œν‘œλ₯Ό 큐에 좔가함
queue = deque([(i, j) for i in range(n) for j in range(m) if tomato_grid[i][j] == 1])

# λ°©ν–₯ 벑터λ₯Ό μ΄ˆκΈ°ν™”ν•¨ (상, ν•˜, 쒌, 우)
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]

# BFS 탐색을 μ‹œμž‘ν•¨
def bfs():
while queue:
x, y = queue.popleft()
for dx, dy in directions:
nx, ny = x + dx, y + dy
# λ²”μœ„ 내에 있고, 읡지 μ•Šμ€ ν† λ§ˆν† (0)일 경우 읡힘
if 0 <= nx < n and 0 <= ny < m and tomato_grid[nx][ny] == 0:
tomato_grid[nx][ny] = tomato_grid[x][y] + 1
queue.append((nx, ny))

# BFSλ₯Ό 싀행함
bfs()

# κ²°κ³Όλ₯Ό 계산함
days = 0
for row in tomato_grid:
# 읡지 μ•Šμ€ ν† λ§ˆν† κ°€ 있으면 -1을 좜λ ₯함
if 0 in row:
print(-1)
exit()
# κ°€μž₯ 큰 값이 κ±Έλ¦° λ‚ μ§œλ₯Ό 계산함
days = max(days, max(row))

# μ‹œμž‘μ„ 1μ—μ„œ ν–ˆμœΌλ―€λ‘œ κ²°κ³Όμ—μ„œ 1을 빼쀌
print(days - 1)
46 changes: 44 additions & 2 deletions janghw0126/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
## ✏️ 기둝
## ✏️ 기둝

### 2024-1
| μ°¨μ‹œ | λ‚ μ§œ | λ¬Έμ œμœ ν˜• | 링크 | 풀이 |
|:----:|:---------:|:----:|:-----:|:----:|
| 1μ°¨μ‹œ | 2024.1.1 | ν•΄μ‹œ | <a href= "https://school.programmers.co.kr/learn/courses/30/lessons/42576">μ™„μ£Όν•˜μ§€ λͺ»ν•œ μ„ μˆ˜</a> |[#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/4) |
| 2μ°¨μ‹œ | 2024.1.4 | κ΅¬ν˜„ | <a href= "https://www.acmicpc.net/problem/1152">λ‹¨μ–΄μ˜ 개수</a> |[#9](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/9) |
| 3μ°¨μ‹œ | 2024.1.7 | κ΅¬ν˜„ | <a href= "https://www.acmicpc.net/problem/1157">단어 곡뢀</a> |[#13](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/13) |
| 4μ°¨μ‹œ | 2024.1.10 | κ΅¬ν˜„ | <a href= "https://www.acmicpc.net/problem/2869">λ‹¬νŒ½μ΄λŠ” μ˜¬λΌκ°€κ³  μ‹Άλ‹€</a> |[#20](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/20) |
| 5μ°¨μ‹œ | 2024.1.13 | μˆ˜ν•™ | <a href= "https://www.acmicpc.net/problem/2581">μ†Œμˆ˜</a> |[#24](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/24) |
| 6μ°¨μ‹œ | 2024.1.16 | μŠ€νƒ | <a href= "https://www.acmicpc.net/problem/28278">μŠ€νƒ 2</a> |[#26](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/26) |
| 7μ°¨μ‹œ | 2024.1.19 | μŠ€νƒ | <a href= "https://www.acmicpc.net/problem/9012">κ΄„ν˜Έ</a> |[#30](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/30) |
| 8μ°¨μ‹œ | 2024.1.23 | μŠ€νƒ | <a href= "https://www.acmicpc.net/problem/1874">μŠ€νƒ μˆ˜μ—΄</a> |[#36](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/36) |
| 9μ°¨μ‹œ | 2024.1.25 | μŠ€νƒ | <a href= "https://www.acmicpc.net/problem/4949">κ· ν˜•μž‘νžŒ 세상</a> |[#40](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/40) |
| 10μ°¨μ‹œ | 2024.1.28 | 큐 | <a href= "https://www.acmicpc.net/problem/10845">큐</a> |[#42](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/42) |
| 11μ°¨μ‹œ | 2024.2.1 | 큐 | <a href= "https://www.acmicpc.net/problem/1966">ν”„λ¦°ν„° 큐</a> |[#48](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/48) |
| 12μ°¨μ‹œ | 2024.2.4 | DFS | <a href= "https://www.acmicpc.net/problem/3109">빡집</a> |[#53](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/53) |
| 13μ°¨μ‹œ | 2024.2.7 | DFS | <a href= "https://www.acmicpc.net/problem/2606">λ°”μ΄λŸ¬μŠ€</a> |[#57](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/57) |
| 14μ°¨μ‹œ | 2024.2.12 | λ°±νŠΈλž˜ν‚Ή | <a href= "https://www.acmicpc.net/problem/15649">Nκ³Ό M (1)</a> |[#61](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/61) |
| 15μ°¨μ‹œ | 2024.2.15 | λ°±νŠΈλž˜ν‚Ή | <a href= "https://www.acmicpc.net/problem/15650">Nκ³Ό M (2)</a> |[#65](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/65) |
| 16μ°¨μ‹œ | 2024.2.18 | λ°±νŠΈλž˜ν‚Ή | <a href= "https://www.acmicpc.net/problem/15654">Nκ³Ό M (5)</a> |[#66](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/66) |
| 17μ°¨μ‹œ | 2024.2.21 | λ°±νŠΈλž˜ν‚Ή | <a href= "https://www.acmicpc.net/problem/15651">Nκ³Ό M (3)</a> |[#72](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/72) |
| 18μ°¨μ‹œ | 2024.2.24 | 그리디 | <a href= "https://www.acmicpc.net/problem/1541">μžƒμ–΄λ²„λ¦° κ΄„ν˜Έ</a> |[#77](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/77) |
| 19μ°¨μ‹œ | 2024.2.27 | 이진 탐색 | <a href= "https://www.acmicpc.net/problem/11663">μ„ λΆ„ μœ„μ˜ 점</a> |[#79](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/79) |
| 20μ°¨μ‹œ | 2024.3.2 | 이진 탐색 | <a href= "https://www.acmicpc.net/problem/1920">수 μ°ΎκΈ°</a> |[#85](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/85) |
| 21μ°¨μ‹œ | 2024.3.5 | μ •μˆ˜λ‘  | <a href= "https://www.acmicpc.net/problem/17425">μ•½μˆ˜μ˜ ν•©</a> |[#89](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/89) |
| 22μ°¨μ‹œ | 2024.3.8 | λΆ„ν•  정볡 | <a href= "https://www.acmicpc.net/problem/1780">μ’…μ΄μ˜ 개수</a> |[#91](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/91) |
| 23μ°¨μ‹œ | 2024.3.13 | λ°±νŠΈλž˜ν‚Ή | <a href= "https://www.acmicpc.net/problem/6603">둜또</a> |[#97](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/97) |
| 24μ°¨μ‹œ | 2024.3.15 | λˆ„μ ν•© | <a href= "https://www.acmicpc.net/problem/20438">μΆœμ„μ²΄ν¬</a> |[#101](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/101) |
| 25μ°¨μ‹œ | 2024.3.26 | λ‹€μ΅μŠ€νŠΈλΌ | <a href= "https://www.acmicpc.net/problem/10191">녹색 옷 μž…μ€ μ• κ°€ 저닀지?</a> |[#108](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/108) |
| 26μ°¨μ‹œ | 2024.3.30 | 브루트 포슀 | <a href= "https://www.acmicpc.net/problem/14501">퇴사</a> |[#110](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/110) |
| 27μ°¨μ‹œ | 2024.4.3 | 브루트 포슀 | <a href= "https://www.acmicpc.net/problem/16198">μ—λ„ˆμ§€ λͺ¨μœΌκΈ°</a> |[#115](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/115) |
| 28μ°¨μ‹œ | 2024.4.12 | μ •λ ¬ | <a href= "https://www.acmicpc.net/problem/2751">수 μ •λ ¬ν•˜κΈ° 2</a> |[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/117) |
---

### 2024-2
| μ°¨μ‹œ | λ‚ μ§œ | λ¬Έμ œμœ ν˜• | 링크 | 풀이 |
|:----:|:---------:|:----:|:-----:|:----:|
| 1μ°¨μ‹œ | 2024.7.17 | μˆ˜ν•™ | <a href= "https://www.acmicpc.net/problem/1241">머리 톑톑</a> |[#124](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/124) |
Expand All @@ -10,4 +44,12 @@
| 6μ°¨μ‹œ | 2024.8.3 | BFS | <a href= "https://www.acmicpc.net/problem/14940">μ‰¬μš΄ μ΅œλ‹¨κ±°λ¦¬</a> |[#140](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/140) |
| 7μ°¨μ‹œ | 2024.8.8 | DFS | <a href= "https://school.programmers.co.kr/learn/courses/30/lessons/92343">μ–‘κ³Ό λŠ‘λŒ€</a> |[#145](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/145) |
| 8μ°¨μ‹œ | 2024.8.19 | μ΅œμ†Œ νž™ | <a href= "https://www.acmicpc.net/problem/2075">N번째 큰 수</a> |[#145](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/147) |
| 9μ°¨μ‹œ | 2024.8.25 | BFS | <a href= "https://www.acmicpc.net/problem/1697">μˆ¨λ°”κΌ­μ§ˆ</a> |[#149](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/149) |
| 9μ°¨μ‹œ | 2024.8.25 | BFS | <a href= "https://www.acmicpc.net/problem/1697">μˆ¨λ°”κΌ­μ§ˆ</a> |[#149](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/149) |
| 10μ°¨μ‹œ | 2024.9.9 | μœ„μƒμ •λ ¬ | <a href= "https://www.acmicpc.net/problem/2252">쀄 μ„Έμš°κΈ°</a> |[#158](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/158) |
| 11μ°¨μ‹œ | 2024.9.17 | μš°μ„ μˆœμœ„ 큐 | <a href= "https://www.acmicpc.net/problem/7662">이쀑 μš°μ„ μˆœμœ„ 큐</a> |[#160](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/160) |
| 12μ°¨μ‹œ | 2024.9.24 | 투 포인터 | <a href= "https://www.acmicpc.net/problem/5525">IOIOI</a> |[#163](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/163) |
| 13μ°¨μ‹œ | 2024.9.27 | BFS | <a href= "https://www.acmicpc.net/problem/7576">ν† λ§ˆν† </a> |[#166](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/166) |
| 14μ°¨μ‹œ | 2024.10.5 | BFS | <a href= "https://www.acmicpc.net/problem/10026">적둝색약</a> |[#172](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/172) |
| 15μ°¨μ‹œ | 2024.10.12 | 이진 탐색 | <a href= "https://school.programmers.co.kr/learn/courses/30/lessons/43236">징검닀리</a> |[#176](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/176) |
| 16μ°¨μ‹œ | 2024.10.30 | μŠ€νƒ | <a href= "https://www.acmicpc.net/problem/9935">λ¬Έμžμ—΄ 폭발</a> |[#183](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/183) |
---
25 changes: 25 additions & 0 deletions janghw0126/μŠ€νƒ/λ¬Έμžμ—΄ 폭발.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys
input=sys.stdin.readline

# λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
string = input().strip()
# 폭발 λ¬Έμžμ—΄ μž…λ ₯ν•˜κΈ°
bomb_string = input().strip()

# 폭발 λ¬Έμžμ—΄ 길이 κ³„μ‚°ν•˜κΈ°
bomb_len=len(bomb_string)

# κ²°κ³Ό 좜λ ₯ 리슀트 μ„ μ–Έ
stack=[]

# stack에 μŒ“μ•„λ‘κ³ , ν­λ°œλ¬Έμžμ—΄μ΄ μžˆλŠ” 경우 pop
for str in string:
stack.append(str)
# 폭발 λ¬Έμžμ—΄κ³Ό stack 뒷뢀뢄이 같은 경우
if ''.join(stack[-bomb_len:])==bomb_string:
# ν•΄λ‹Ή λ¬Έμžμ—΄ pop
for _ in range(bomb_len):
stack.pop()
Comment on lines +16 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

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

''.join()을 μ‚¬μš©ν•˜μ—¬ λ¬Έμžμ—΄μ„ λΉ„κ΅ν•˜λŠ” 뢀뢄을 μ΅œμ ν™”ν•˜μ—¬ μ‹œκ°„μ„ 쑰금 더 μ€„μ—¬λ³΄μ•˜μŠ΅λ‹ˆλ‹€ !

  • 폭발 λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰ 문자λ₯Ό λ¨Όμ € ν™•μΈν•˜μ—¬ λΆˆν•„μš”ν•œ λ¬Έμžμ—΄ 비ꡐλ₯Ό μ€„μ˜€μŠ΅λ‹ˆλ‹€.
  • ''.join() ν˜ΈμΆœμ„ μ΅œμ†Œν™”ν–ˆμŠ΅λ‹ˆλ‹€.
for char in string:
    stack.append(char)
    
    # ν˜„μž¬ λ¬Έμžκ°€ 폭발 λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰ λ¬Έμžμ™€ κ°™κ³ 
    # μŠ€νƒμ˜ 길이가 폭발 λ¬Έμžμ—΄ 이상일 λ•Œλ§Œ 검사
    if char == last_char and len(stack) >= bomb_len:
        # 폭발 λ¬Έμžμ—΄κ³Ό μΌμΉ˜ν•˜λŠ”μ§€ 직접 비ꡐ
        is_bomb = True
        for i in range(bomb_len):
            if stack[-(bomb_len-i)] != bomb[i]:
                is_bomb = False
                break
        
        if is_bomb:
            del stack[-bomb_len:]  # pop() λŒ€μ‹  del μ‚¬μš©ν•˜μ—¬ ν•œλ²ˆμ— 제거

print(''.join(stack) if stack else "FRULA", end='')

μ›λž˜ μ½”λ“œλ³΄λ‹€ 쑰금 더 μ‹œκ°„μ΄ 빨라진 것을 확인할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ !
image


# κ²°κ³Ό 좜λ ₯
print(''.join(stack) if stack else "FRULA",end='')
39 changes: 39 additions & 0 deletions janghw0126/μœ„μƒμ •λ ¬/쀄 μ„Έμš°κΈ°.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
from collections import deque

n, m = map(int, sys.stdin.readline().split())

# κ·Έλž˜ν”„μ™€ μ§„μž… 차수 리슀트λ₯Ό μ΄ˆκΈ°ν™”ν•¨
graph = [[] for _ in range(n + 1)]
deg = [0] * (n + 1)
q = deque()
res = []

# 비ꡐ 관계 μž…λ ₯κ³Ό κ·Έλž˜ν”„λ₯Ό ꡬ성함
for _ in range(m):
u, v = map(int, sys.stdin.readline().rstrip().split())
# uκ°€ v μ•žμ— μ„œμ•Ό 함
graph[u].append(v)
# v의 μ§„μž… 차수λ₯Ό μ¦κ°€μ‹œν‚΄
deg[v] += 1

# μ§„μž… μ°¨μˆ˜κ°€ 0인 λ…Έλ“œ 큐에 μ‚½μž…ν•¨
for i in range(1, n + 1):
if deg[i] == 0:
q.append(i)

# μœ„μƒ 정렬을 μ‹œμž‘ν•¨
while q:
# μ§„μž… μ°¨μˆ˜κ°€ 0인 λ…Έλ“œλ₯Ό νμ—μ„œ 꺼냄
cur = q.popleft()
# κ²°κ³Ό λ¦¬μŠ€νŠΈμ— 좔가함
res.append(cur)

# ν˜„μž¬ λ…Έλ“œ 뒀에 μ„œμ•Ό ν•˜λŠ” λ…Έλ“œλ“€μ˜ μ§„μž… 차수λ₯Ό κ°μ†Œμ‹œν‚΄
for next_node in graph[cur]:
deg[next_node] -= 1
# μ§„μž… μ°¨μˆ˜κ°€ 0이 되면 큐에 좔가함
if deg[next_node] == 0:
q.append(next_node)

print(*res)
23 changes: 23 additions & 0 deletions janghw0126/이진 탐색/징검닀리.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def solution(distance, rocks, n):
rocks.sort()
rocks.append(distance)
left, right = 1, distance

while left <= right:
mid = (left+right) // 2
current = 0
removed_rocks = 0

for rock in rocks:
if rock-current < mid:
removed_rocks += 1
else:
current = rock

if removed_rocks > n:
right = mid-1
else:
answer = mid
left = mid+1

return answer
32 changes: 32 additions & 0 deletions janghw0126/투 포인터/IOIOI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys
input = sys.stdin.readline

n = int(input())
m = int(input())
sequence = input().rstrip()

# 투 포인터 μ•Œκ³ λ¦¬μ¦˜μ„ μœ„ν•΄μ„œ 쒌우 포인터 μ„ μ–Έ
start, current = 0, 0
# 'IOI' νŒ¨ν„΄μ„ 찾은 횟수 μ„ μ–Έ
pattern_count = 0

# λ¬Έμžμ—΄ λ²”μœ„ λ‚΄μ—μ„œ 반볡
while current < m:
# ν˜„μž¬ μœ„μΉ˜μ—μ„œ 'IOI' νŒ¨ν„΄μ΄ 발견된 경우
if sequence[current:current + 3] == 'IOI':
# νŒ¨ν„΄μ„ 찾으면 포인터λ₯Ό 두 μΉΈμ”© 이동
current += 2
# νŒ¨ν„΄μ˜ 길이가 'N'에 λ§žλŠ” 경우
if current - start == 2 * n:
# νŒ¨ν„΄ 카운트 증가
pattern_count += 1
# νŒ¨ν„΄μ„ μ™„μ„±ν–ˆμœΌλ‹ˆ μ‹œμž‘ 포인터도 두 μΉΈ 이동
start += 2
else:
# νŒ¨ν„΄μ΄ λ§žμ§€ μ•ŠμœΌλ©΄ ν•œ μΉΈμ”© 이동
current += 1
# μ‹œμž‘ 포인터λ₯Ό ν˜„μž¬ 포인터 μœ„μΉ˜λ‘œ μž¬μ„€μ •
start = current

# νŒ¨ν„΄ 횟수 좜λ ₯
print(pattern_count)
42 changes: 42 additions & 0 deletions janghw0126/νž™/이쀑 μš°μ„ μˆœμœ„ 큐.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sys
import heapq

def solve():
input = sys.stdin.readline
T = int(input())

for _ in range(T):
min_heap = [] # μ΅œμ†Œ νž™
max_heap = [] # μ΅œλŒ€ νž™ (음수둜 λ³€ν™˜ν•˜μ—¬ μ €μž₯)
count = int(input()) # λͺ…λ Ήμ–΄ 수
status = [1] * count # μž‘μ—… μƒνƒœ 좔적 (1: 유효, 0: μ‚­μ œλ¨)

for i in range(count):
command, value = input().split()
value = int(value)

if command == "I": # μ‚½μž… μ—°μ‚°
heapq.heappush(min_heap, (value, i))
heapq.heappush(max_heap, (-value, i))
elif command == "D": # μ‚­μ œ μ—°μ‚°
if value == -1: # μ΅œμ†Œκ°’ μ‚­μ œ
if min_heap:
status[heapq.heappop(min_heap)[1]] = 0
elif value == 1: # μ΅œλŒ€κ°’ μ‚­μ œ
if max_heap:
status[heapq.heappop(max_heap)[1]] = 0

# μœ νš¨ν•˜μ§€ μ•Šμ€ κ°’ 제거
while min_heap and status[min_heap[0][1]] == 0:
heapq.heappop(min_heap)
while max_heap and status[max_heap[0][1]] == 0:
heapq.heappop(max_heap)

# κ²°κ³Ό 좜λ ₯
if not min_heap or not max_heap:
print("EMPTY")
else:
print(-max_heap[0][0], min_heap[0][0])

if __name__ == "__main__":
solve()