728x90
๋ฐ์ํ
ํ๋ก๊ทธ๋๋จธ์ค "๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ" ๋ฌธ์ ๋ฅผ ์ฐ์ ์์ ํ๋ก ํ์์๋๋ฐ, ๋์ผํ ๊ธฐ๋ฅ์ ํ๋ ํ์ด์ฌ ๋ด์ฅ heapq๋ฅผ ์ฌ์ฉํ๋ฉด ๋ ๋น ๋ฅด๊ฒ ์๋ฌด๋ฅผ ์ํํ๋ค๊ณ ํ๋ค. ๊ทธ๋์ ์ฐ์ ์์ํ๋ฅผ ํํ๋ก ๋ณ๊ฒฝํด๋ณด๊ธฐ๋ก ํ๋ค.
https://inspirer9.tistory.com/400
์ฐ์ ์์ํ๋ฅผ ํํ๋ก ๋ณ๊ฒฝํ๋ ์ด์
- ๋ ๋ค O(logN)์ด๊ณ ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ฌ์ฉ๋๋๋ฐ, ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ์์๋ heapq๋ฅผ ์ฐ๋๊ฒ ์ข๋ค๊ณ ํ๋ค.
- heapq๊ฐ PriorityQueue๋ณด๋ค ์คํ์๊ฐ์ด ์ ๊ฒ ๊ฑธ๋ฆฐ๋ค.
์ฐ์ ์์ํ๋ฅผ ํํ๋ก ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ
- ์ฐ์ ์์ํ๋ฅผ ํํ๋ก ๋ฐ๊พธ๋ ค๋ฉด ์ผ์ชฝ ํญ๋ชฉ๋ค์ ์ฐ์ธก ํญ๋ชฉ์ผ๋ก ๋ณ๊ฒฝํ๋ฉด ๋๋ค.
PriorityQueue | heapq |
from queue import PriorityQueue pq = PriorityQueue() pq.put(1) pq.put(3) pq.put(2) pq.get() # 1 pq.get() # 2 pq.get() # 3 |
import heapq pq = [] heapq.heappush(pq, 1) heapq.heappush(pq, 3) heapq.heappush(pq, 2) heapq.heappop(pq) # 1 heapq.heappop(pq) # 2 heapq.heappop(pq) # 3 |
์ฐ์ ์์ํ ๊ฟํ
- PriorityQueue์ heapq๋ ์ต์ ํ๋ง ๋ค๋ฃฐ ์ ์๊ธฐ ๋๋ฌธ์ ํฐ ์๋ถํฐ pop ํ๊ณ ์ถ์ ๋๋ - ๋ถํธ๋ฅผ ๋ถํ push/popํ๋ค.
์์ค์ฝ๋
์ฐ์ ์์ ํ (PriorityQueue)
from queue import PriorityQueue
def solution(maps):
#if maps[0][0] == 0: #์ด์ ๋ณด๋ ์ด ๋ถ๋ถ ํ์์๋ค?
# return -1
pq = PriorityQueue() #๊ทธ๋ฅ ํ๋ฅผ ์จ๋ ๋๋ ๋ฏ... ใ
ก.ใ
ก;
dist = 1
sx, sy = 0, 0
ex, ey = len(maps[0]), len(maps)
INF = 10001 #float์ ์ธ ํ์๊ฐ ์์.
#float("inf") #ํ์ด์ฌ 3.5 ์ด์์์ ์ฌ์ฉ ๊ฐ๋ฅ
for y in range(ey):
for x in range(ex):
if maps[y][x] != 0:
maps[y][x] = INF
pq.put([dist,(sx,sy)])
while pq.empty() == False:
t = pq.get()
dist = t[0]
x = t[1][0]
y = t[1][1]
if y == ey-1 and x == ex-1:
return dist
if dist < maps[y][x]:
maps[y][x] = dist
dist += 1
if sx < x and dist < maps[y][x-1]:
pq.put([dist,(x-1,y)])
if x < ex-1 and dist < maps[y][x+1]:
pq.put([dist,(x+1,y)])
if sy < y and dist < maps[y-1][x]:
pq.put([dist,(x,y-1)])
if y < ey-1 and dist < maps[y+1][x]:
pq.put([dist,(x,y+1)])
return -1
ํ ํ (heapq)
import heapq
def solution(maps):
#if maps[0][0] == 0: #์ด์ ๋ณด๋ ์ด ๋ถ๋ถ ํ์์๋ค?
# return -1
pq = []
dist = 1
sx, sy = 0, 0
ex, ey = len(maps[0]), len(maps)
INF = 10001 #float์ ์ธ ํ์๊ฐ ์์.
#float("inf") #ํ์ด์ฌ 3.5 ์ด์์์ ์ฌ์ฉ ๊ฐ๋ฅ
for y in range(ey):
for x in range(ex):
if maps[y][x] != 0:
maps[y][x] = INF
heapq.heappush(pq, [dist,(sx,sy)])
while len(pq) > 0:
t = heapq.heappop(pq)
dist = t[0]
x = t[1][0]
y = t[1][1]
if y == ey-1 and x == ex-1:
return dist
if dist < maps[y][x]:
maps[y][x] = dist
dist += 1
if sx < x and dist < maps[y][x-1]:
heapq.heappush(pq, [dist,(x-1,y)])
if x < ex-1 and dist < maps[y][x+1]:
heapq.heappush(pq, [dist,(x+1,y)])
if sy < y and dist < maps[y-1][x]:
heapq.heappush(pq, [dist,(x,y-1)])
if y < ey-1 and dist < maps[y+1][x]:
heapq.heappush(pq, [dist,(x,y+1)])
return -1
๋ ํ(deque)
- ์๋ฌธ์๋ ๋ ๋น ๋ฅด๋ค๋ ๋ํ๊ฐ ์์ด์ ๋ํ๋ก๋ ๋ฐ๊ฟ๋ณด์๋ค.
from collections import deque
def solution(maps):
q = deque()
dist = 1
sx, sy = 0, 0
ex, ey = len(maps[0]), len(maps)
INF = 10001
for y in range(ey):
for x in range(ex):
if maps[y][x] != 0:
maps[y][x] = INF
q.append((dist, (sx, sy)))
while q:
dist, (x, y) = q.popleft()
if y == ey - 1 and x == ex - 1:
return dist
if dist < maps[y][x]:
maps[y][x] = dist
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = x + dx, y + dy
if nx >= 0 and nx < ex and ny >= 0 and ny < ey and dist + 1 < maps[ny][nx]:
q.append((dist + 1, (nx, ny)))
return -1
์๋ ๋น๊ต
- ๋นจ๋ผ์ก๋ค. heapq๊ฐ ๋ ๋น ๋ฅด๋ค. ๊ทผ๋ฐ deque๊ฐ ๋ ๋น ๋ฅด๋ค.
PriorityQueue | heapq | deque |
์ ํ์ฑ ํ
์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.10ms, 10.4MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.06ms, 10.4MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.13ms, 10.5MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.09ms, 10.3MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.20ms, 10.4MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.27ms, 10.4MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.23ms, 10.3MB) ํ ์คํธ 8 ใ ํต๊ณผ (0.13ms, 10.3MB) ํ ์คํธ 9 ใ ํต๊ณผ (0.20ms, 10.3MB) ํ ์คํธ 10 ใ ํต๊ณผ (0.21ms, 10.4MB) ํ ์คํธ 11 ใ ํต๊ณผ (0.10ms, 10.4MB) ํ ์คํธ 12 ใ ํต๊ณผ (0.08ms, 10.3MB) ํ ์คํธ 13 ใ ํต๊ณผ (0.12ms, 10.4MB) ํ ์คํธ 14 ใ ํต๊ณผ (0.11ms, 10.3MB) ํ ์คํธ 15 ใ ํต๊ณผ (0.11ms, 10.4MB) ํ ์คํธ 16 ใ ํต๊ณผ (0.06ms, 10.3MB) ํ ์คํธ 17 ใ ํต๊ณผ (0.17ms, 10.3MB) ํ ์คํธ 18 ใ ํต๊ณผ (0.08ms, 10.3MB) ํ ์คํธ 19 ใ ํต๊ณผ (0.06ms, 10.1MB) ํ ์คํธ 20 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 21 ใ ํต๊ณผ (0.04ms, 10.3MB) ํจ์จ์ฑ ํ ์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (84.16ms, 10.6MB) ํ ์คํธ 2 ใ ํต๊ณผ (14.37ms, 10.5MB) ํ ์คํธ 3 ใ ํต๊ณผ (39.36ms, 10.6MB) ํ ์คํธ 4 ใ ํต๊ณผ (22.83ms, 10.4MB) |
์ ํ์ฑ ํ
์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.02ms, 10.4MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.04ms, 10.2MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.05ms, 10.3MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.04ms, 10.2MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.11ms, 10.4MB) ํ ์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 9 ใ ํต๊ณผ (0.09ms, 10.2MB) ํ ์คํธ 10 ใ ํต๊ณผ (0.05ms, 10.3MB) ํ ์คํธ 11 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 12 ใ ํต๊ณผ (0.04ms, 10.3MB) ํ ์คํธ 13 ใ ํต๊ณผ (0.03ms, 10.4MB) ํ ์คํธ 14 ใ ํต๊ณผ (0.05ms, 10.2MB) ํ ์คํธ 15 ใ ํต๊ณผ (0.04ms, 10.3MB) ํ ์คํธ 16 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 17 ใ ํต๊ณผ (0.06ms, 10.3MB) ํ ์คํธ 18 ใ ํต๊ณผ (0.01ms, 10.3MB) ํ ์คํธ 19 ใ ํต๊ณผ (0.01ms, 10.4MB) ํ ์คํธ 20 ใ ํต๊ณผ (0.01ms, 10.3MB) ํ ์คํธ 21 ใ ํต๊ณผ (0.01ms, 10.4MB) ํจ์จ์ฑ ํ ์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (23.62ms, 10.1MB) ํ ์คํธ 2 ใ ํต๊ณผ (4.68ms, 10.1MB) ํ ์คํธ 3 ใ ํต๊ณผ (11.67ms, 10.2MB) ํ ์คํธ 4 ใ ํต๊ณผ (6.93ms, 10.4MB) |
์ ํ์ฑ ํ
์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (0.04ms, 10.1MB) ํ ์คํธ 2 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 3 ใ ํต๊ณผ (0.06ms, 10.3MB) ํ ์คํธ 4 ใ ํต๊ณผ (0.04ms, 10.2MB) ํ ์คํธ 5 ใ ํต๊ณผ (0.04ms, 10.3MB) ํ ์คํธ 6 ใ ํต๊ณผ (0.07ms, 10.1MB) ํ ์คํธ 7 ใ ํต๊ณผ (0.09ms, 10.1MB) ํ ์คํธ 8 ใ ํต๊ณผ (0.05ms, 10.2MB) ํ ์คํธ 9 ใ ํต๊ณผ (0.09ms, 10MB) ํ ์คํธ 10 ใ ํต๊ณผ (0.05ms, 10.3MB) ํ ์คํธ 11 ใ ํต๊ณผ (0.03ms, 10.2MB) ํ ์คํธ 12 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 13 ใ ํต๊ณผ (0.06ms, 10.3MB) ํ ์คํธ 14 ใ ํต๊ณผ (0.03ms, 10.1MB) ํ ์คํธ 15 ใ ํต๊ณผ (0.03ms, 10.3MB) ํ ์คํธ 16 ใ ํต๊ณผ (0.02ms, 10.2MB) ํ ์คํธ 17 ใ ํต๊ณผ (0.08ms, 10.1MB) ํ ์คํธ 18 ใ ํต๊ณผ (0.01ms, 10.2MB) ํ ์คํธ 19 ใ ํต๊ณผ (0.01ms, 10.2MB) ํ ์คํธ 20 ใ ํต๊ณผ (0.01ms, 10.2MB) ํ ์คํธ 21 ใ ํต๊ณผ (0.02ms, 10.3MB) ํจ์จ์ฑ ํ ์คํธ ํ ์คํธ 1 ใ ํต๊ณผ (11.94ms, 10.2MB) ํ ์คํธ 2 ใ ํต๊ณผ (3.38ms, 10.3MB) ํ ์คํธ 3 ใ ํต๊ณผ (8.15ms, 10.4MB) ํ ์คํธ 4 ใ ํต๊ณผ (5.36ms, 10.3MB) |
Priority Queue, heapq, deque ๊ตฌํ๋ฐฉ์์ ์ฐจ์ด์
- ๋๋์ฒด ๋ญ ์ฐจ์ด๊ฐ ์์ด์ ์ด๋ฐ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋๊ฑด๊ฐ?
PriorityQueue | heapq | deque |
ํด๋์ค | ๋ชจ๋ (๋ฉํฐ์ฐ๋ ๋ ์ง์) | ๋ชจ๋(Collections) |
์ต์ํ heap์ ๋์ผํ ๊ธฐ๋ฅ ์๋๊ฐ ๋ ๋๋ฆผ |
์ต์ํ, ์ต๋ํ(-์ฌ์ฉ) ๋ค์ต์คํธ๋ผ, ์ต์๊ฐ์ด๋ ์ต๋๊ฐ์ ๋นจ๋ฆฌ ์ฐพ์์ผ ํ ๋ ์ฌ์ฉ |
์ ์
์ ์ถ, ํ์
ํ์ถ, ์ ์
ํ์ถ, ํ์
์ ์ถ ๋ค๋จ. BFS์ ์ฌ์ฉ |
- ์ ์ ํ์ง ์์ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ ๊ฒ์ผ๋ก ๊ฒฐ๋ก ใ ใ ใ
728x90
๋ฐ์ํ
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ์ธ์ฌ๊ณ ๊ณผ (0) | 2023.02.14 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ (0) | 2023.02.12 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ๊ณ ๋์ Kit (2) ํด์ (0) | 2023.02.09 |
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ๊ณ ๋์ Kit (1) (0) | 2023.02.08 |
ํ์ด์ฌ ์ฐ์ ์์ํ, ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ, ๊ธธ์ฐพ๊ธฐ (0) | 2023.02.08 |