일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 715. Range Module
- Decorator
- DWG
- 컴퓨터의 구조
- concurrency
- Class
- 프로그래머스
- Python Implementation
- Regular Expression
- 파이썬
- 운영체제
- Python
- Protocol
- 30. Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- Generator
- Substring with Concatenation of All Words
- t1
- Python Code
- 315. Count of Smaller Numbers After Self
- data science
- iterator
- LeetCode
- 43. Multiply Strings
- kaggle
- 시바견
- 109. Convert Sorted List to Binary Search Tree
- 밴픽
- attribute
- shiba
Archives
- Today
- Total
Scribbling
프로그래머스: 섬 연결하기 본문
섬을 연결하는 최소 비용을 구하는 문제로, 최소 신장 트리 문제에 해당된다.
자연스럽게 크루스칼 알고리즘을 떠올릴 수 있다.
최소 신장 트리 혹은 크루스칼 알고리즘이 궁금하다면, 아래 링크로!
(간단히 말하자면, 크루스칼 알고리즘은 비용이 가장 적게 드는 edge부터 연결시켜가는 알고리즘이다)
https://focalpoint.tistory.com/15
신장 트리, 최소 신장 트리, Kruskal Algorithm
1. 신장 트리: 모든 node를 포함하면서 cycle이 존재하지 않는 그래프를 의미한다. 2. 최소 신장 트리: 신장 트리 중에서 최소 비용으로 만들 수 있는 트리 3. Kruskal Algorithm: 최소 신장 트리를 찾는 알
focalpoint.tistory.com
def solution(n, costs):
def find_parent(parent, x):
if parent[x] == x:
return x
parent[x] = find_parent(parent, parent[x])
return parent[x]
def union(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
answer = 0
parent = [i for i in range(n)]
costs.sort(key=lambda x: x[2])
for cost in costs:
a, b, c = cost
if find_parent(parent, a) != find_parent(parent, b):
union(parent, a, b)
answer += c
return answer
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 106. Construct Binary Tree from Inorder and Postorder Traversal (0) | 2021.10.21 |
---|---|
프로그래머스: 단속카메라 (0) | 2021.10.21 |
프로그래머스: 구명보트 (0) | 2021.10.21 |
프로그래머스: 큰 수 만들기 (0) | 2021.10.20 |
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal (0) | 2021.10.20 |