일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Generator
- 시바견
- 밴픽
- shiba
- Python
- concurrency
- Convert Sorted List to Binary Search Tree
- t1
- 운영체제
- 프로그래머스
- LeetCode
- attribute
- 파이썬
- 43. Multiply Strings
- Protocol
- Class
- Python Code
- 315. Count of Smaller Numbers After Self
- iterator
- kaggle
- data science
- DWG
- 30. Substring with Concatenation of All Words
- Regular Expression
- Python Implementation
- Decorator
- Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- 컴퓨터의 구조
Archives
- Today
- Total
Scribbling
프로그래머스: 가장 먼 노드 본문
다익스트라 알고리즘을 사용한다.
참고: https://focalpoint.tistory.com/6
최단 거리 알고리즘
최단 거리 알고리즘을 정리해보자. 1. 다익스트라 알고리즘 - 하나의 node로부터 다른 모든 node까지의 최단거리를 계산 가능 - 음의 간선이 없는 경우에만 유효 - O(VlogV) import heapq def dijkstra(start): pq
focalpoint.tistory.com
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 |