Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
33 changes: 33 additions & 0 deletions janghw0126/BFS/์ˆจ๋ฐ”๊ผญ์งˆ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
from collections import deque

def bfs_find_min_time(start, target, max_pos):
# BFS๋ฅผ ์œ„ํ•œ ํ๋ฅผ ์ƒ์„ฑํ•จ
queue = deque([start])

while queue:
current_pos = queue.popleft()

# ํ˜„์žฌ ์œ„์น˜๊ฐ€ ๋ชฉํ‘œ ์œ„์น˜์™€ ๊ฐ™์œผ๋ฉด, ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์—์„œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•จ
if current_pos == target:
print(time_taken[current_pos])
return

# ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ์„ธ ๊ฐ€์ง€ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•จ
for next_pos in (current_pos - 1, current_pos + 1, current_pos * 2):
# ๋‹ค์Œ ์œ„์น˜๊ฐ€ ๋ฒ”์œ„ ๋‚ด์— ์žˆ๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ์œ„์น˜์ผ ๊ฒฝ์šฐ
if 0 <= next_pos <= max_pos and time_taken[next_pos] == 0:
# ํ˜„์žฌ ์œ„์น˜์—์„œ 1์ดˆ๊ฐ€ ์ถ”๊ฐ€๋œ ์‹œ๊ฐ„์„ ์ €์žฅํ•จ
time_taken[next_pos] = time_taken[current_pos] + 1
# ๋‹ค์Œ ์œ„์น˜๋ฅผ ํ์— ์ถ”๊ฐ€ํ•จ
queue.append(next_pos)

# ์ดˆ๊ธฐ ์„ค์ •์„ ํ•จ (๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ตœ๋Œ€ ์œ„์น˜ ๊ฐ’์„ ๋œปํ•จ)
MAX_POSITION = 100000
# ์ˆ˜๋นˆ์ด์˜ ์‹œ์ž‘ ์œ„์น˜ n๊ณผ ๋™์ƒ์˜ ์œ„์น˜ k๋ฅผ ์ž…๋ ฅ๋ฐ›์Œ
n, k = map(int, sys.stdin.readline().split())
# ๊ฐ ์œ„์น˜๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ธฐ๋กํ•˜๋Š” ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•จ
time_taken = [0] * (MAX_POSITION + 1)

# BFS๋ฅผ ์‹คํ–‰ํ•จ
bfs_find_min_time(n, k, MAX_POSITION)
40 changes: 40 additions & 0 deletions janghw0126/DFS/์–‘๊ณผ ๋Š‘๋Œ€.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ์ „์—ญ ๋ณ€์ˆ˜๋กœ ์ตœ๋Œ€ ์–‘์˜ ์ˆ˜๋ฅผ ์ €์žฅ
max_count = 0

def solution(info, edges):
# ๊ฐ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ ์ดˆ๊ธฐํ™”
child = [[] for _ in range(len(info))]

# edges์— ๋”ฐ๋ผ ๋ถ€๋ชจ-์ž์‹ ๊ด€๊ณ„๋ฅผ ์„ค์ •
for p, c in edges:
child[p].append(c)

# ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์žฌ๊ท€ ํ•จ์ˆ˜ ์ •์˜
def dfs(node, sheep, wolf, next_nodes):
global max_count

# ๋Š‘๋Œ€ ์ˆ˜๊ฐ€ ์–‘ ์ˆ˜๋ณด๋‹ค ๋งŽ์•„์ง€๋ฉด ํƒ์ƒ‰ ์ค‘์ง€
if wolf >= sheep:
return

# ์ตœ๋Œ€ ์–‘์˜ ์ˆ˜๋ฅผ ๊ฐฑ์‹ 
max_count = max(max_count, sheep)

# ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ ๊ฐ€๋Šฅ ๋…ธ๋“œ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
next_nodes.extend(child[node])

# ๋‹ค์Œ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ๋“ค์„ ์ˆœํšŒ
for next_node in next_nodes:
# ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ํ˜„์žฌ ์œ„์น˜๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์—๋งŒ ์ด๋™
new_next_nodes = [n for n in next_nodes if n != next_node]
# ๋‹ค์Œ ๋…ธ๋“œ์— ๋Š‘๋Œ€๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ
if info[next_node]:
dfs(next_node, sheep, wolf + 1, new_next_nodes)
# ๋‹ค์Œ ๋…ธ๋“œ์— ์–‘์ด ์žˆ๋Š” ๊ฒฝ์šฐ
else:
dfs(next_node, sheep + 1, wolf, new_next_nodes)

# ๋ฃจํŠธ ๋…ธ๋“œ(0๋ฒˆ)์—์„œ DFS ์‹œ์ž‘ (์ฒ˜์Œ์— ์–‘ 1๋งˆ๋ฆฌ๊ฐ€ ์žˆ์Œ)
dfs(0, 1, 0, child[0])

return max_count
7 changes: 6 additions & 1 deletion janghw0126/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@
| 4์ฐจ์‹œ | 2024.7.28 | ๋ถ„ํ•  ์ •๋ณต | <a href= "https://www.acmicpc.net/problem/1074">Z</a> |[#135](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/135) |
| 5์ฐจ์‹œ | 2024.7.31 | ์œ„์ƒ์ •๋ ฌ | <a href= "https://www.acmicpc.net/problem/2056">์ž‘์—…</a> |[#138](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/138) |
| 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) |
| 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) |
---
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)
24 changes: 24 additions & 0 deletions janghw0126/ํž™/N๋ฒˆ์งธ ํฐ ์ˆ˜.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys, heapq

input = sys.stdin.readline
# ํ–‰๋ ฌ์˜ ํฌ๊ธฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์Œ
N = int(input())
min_heap = []

for _ in range(N):
row = list(map(int, input().split()))

# ์ฒซ ๋ฒˆ์งธ ์ž…๋ ฅ ์‹œ ํž™์„ ์ดˆ๊ธฐํ™” ํ•จ
if not min_heap:
for num in row:
heapq.heappush(min_heap, num)
else:
for num in row:
# ํž™์˜ ์ตœ์†Ÿ๊ฐ’๋ณด๋‹ค ํ˜„์žฌ ์ˆซ์ž๊ฐ€ ํฌ๋‹ค๋ฉด
if min_heap[0] < num:
heapq.heappush(min_heap, num)
# ํž™์˜ ํฌ๊ธฐ๋ฅผ N์œผ๋กœ ์œ ์ง€ํ•จ
heapq.heappop(min_heap)

# N๋ฒˆ์งธ ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•จ
print(min_heap[0])
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()