๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ด๐ป/[๐๐ฒ๐ญ๐ก๐จ๐ง] ๐๐ฅ๐ ๐จ๐ซ๐ข๐ญ๐ก๐ฆ
[Python] ํ๋ก๊ทธ๋๋จธ์ค ์์ด ๋๋ง์๊ธฐ & ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฐ๋ฌ
๐คRyusun๐ค
2023. 9. 5. 11:30
#ํ๋ก๊ทธ๋๋จธ์ค ์์ด ๋๋ง์๊ธฐ
def solution(n, words):
i = 0
memo = set()
cnt = 1
while True:
if i == len(words):
return [0,0]
elif words[i] in memo :
return [i%n+1, cnt]
elif i != 0 and words[i][0] != words[i-1][-1]:
return [i % n + 1, cnt]
if i%n == n-1:
cnt += 1
memo.add(words[i])
i += 1
# ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฐ๋ฌ
import heapq
def solution(N, road, K):
graph = [[] for _ in range(N+1)]
for i in range(len(road)):
a,b,c = road[i]
graph[a].append([b,c])
graph[b].append([a,c])
min_dist = [1e9] * (N+1)
return dijkstra(graph, min_dist, K)
def dijkstra(graph, min_dist, K):
queue = []
heapq.heappush(queue, [0, 1])
min_dist[1] = 0
while queue:
dist, node = heapq.heappop(queue)
if min_dist[node] > dist:
min_dist[node] = dist
for next_node, weight in graph[node]:
cost = min_dist[node] + weight
if cost < min_dist[next_node]:
min_dist[next_node] = cost
heapq.heappush(queue, [cost, next_node])
cnt = 0
for i in min_dist:
if i <= K:
cnt += 1
return cnt