프로그래머스 파이썬 가장 먼 노드
https://programmers.co.kr/learn/courses/30/lessons/49189
코딩테스트 연습 - 가장 먼 노드
6 [[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] 3
programmers.co.kr
https://pybeginner.tistory.com/89
프로그래머스 파이썬 배달
https://programmers.co.kr/learn/courses/30/lessons/12978 코딩테스트 연습 - 배달 5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4..
pybeginner.tistory.com
아주 그냥 똑같다
다만 다른점이라면 두번째로 큰 값을 어떻게 찾지? 라는 생각으로 검색을 좀 돌려봤는데
https://shoark7.github.io/programming/algorithm/second-largest-number-in-array
숫자 배열에서 두 번째로 큰 값 찾기
숫자 배열에서 두 번째로 큰 값을 찾는 알고리즘을 살펴봅니다. 이에 그치지 않고 K번째 큰 값을 찾는 알고리즘으로까지 확장합니다.
shoark7.github.io
정말 생각의 깊이가 다르신 분이었다..
for문을 돌려서 계속 비교를하면 끝이긴했지만 거기서 좀 더 좋은 방향을 찾기위해서 트라이하시는 부분이 입이 쩍 벌어졌다.
import sys
import heapq
def solution(n, vertex):
INF = sys.maxsize
dp=[INF]*(n+1)
heap =[]
edge = [[] for i in range(n+1)]
for i in vertex:
edge[i[0]].append((1,i[1]))
edge[i[1]].append((1,i[0]))
def dijkstra(start):
dp[start] = 0
heapq.heappush(heap,(0,start))
while heap:
a, now = heapq.heappop(heap)
if dp[now] < a:
continue
for w, node in edge[now]:
next_a = w + a
if next_a < dp[node]:
dp[node] = next_a
heapq.heappush(heap,(next_a,node))
def second(arr):
two = one = -float('inf')
for n in arr:
if n > one:
two = one
one = n
elif two < n < one:
two = n
return two
dijkstra(1)
search = second(dp)
return dp.count(search)
전 포스트와 거의 동일하니 설명은 생략함.