일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬
- shiba
- 밴픽
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- DWG
- iterator
- 프로그래머스
- Decorator
- data science
- Class
- LeetCode
- Protocol
- 컴퓨터의 구조
- kaggle
- t1
- Python Implementation
- Substring with Concatenation of All Words
- Regular Expression
- 시바견
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Python Code
- attribute
- Python
- concurrency
- 43. Multiply Strings
- 315. Count of Smaller Numbers After Self
- Generator
Archives
- Today
- Total
Scribbling
프로그래머스: 가장 먼 노드 본문
다익스트라 알고리즘을 사용한다.
참고: https://focalpoint.tistory.com/6
import heapq
def solution(n, edge):
answer = 0
max_dist = 0
graph = [[] * (n+1) for _ in range(n+1)]
for e in edge:
u, v = e
graph[u].append(v)
graph[v].append(u)
distance = [int(1e9)] * (n+1)
pq = []
heapq.heappush(pq, (0, 1))
distance[1] = 0
while pq:
dist_u, u = heapq.heappop(pq)
if dist_u > max_dist:
answer = 1
max_dist = dist_u
elif dist_u == max_dist:
answer += 1
if distance[u] < dist_u:
continue
for v in graph[u]:
if distance[v] > dist_u + 1:
distance[v] = dist_u + 1
heapq.heappush(pq, (distance[v], v))
return answer
'Computer Science > Coding Test' 카테고리의 다른 글
프로그래머스: 방의 개수 (0) | 2021.11.08 |
---|---|
LeetCode: 127. Word Ladder (0) | 2021.11.07 |
LeetCode: 124. Binary Tree Maximum Path Sum (0) | 2021.11.04 |
LeetCode: 188. Best Time to Buy and Sell Stock IV (0) | 2021.11.04 |
LeetCode: 123. Best Time to Buy and Sell Stock III (0) | 2021.11.03 |