일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Generator
- Regular Expression
- concurrency
- Protocol
- 운영체제
- Python Code
- 프로그래머스
- Substring with Concatenation of All Words
- LeetCode
- 파이썬
- 715. Range Module
- DWG
- 43. Multiply Strings
- 컴퓨터의 구조
- 밴픽
- attribute
- kaggle
- t1
- iterator
- Convert Sorted List to Binary Search Tree
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- Class
- Decorator
- Python
- shiba
- 315. Count of Smaller Numbers After Self
- 시바견
- data science
Archives
- Today
- Total
Scribbling
LeetCode: 1584. Min Cost to Connect All Points 본문
Computer Science/Coding Test
LeetCode: 1584. Min Cost to Connect All Points
focalpoint 2021. 8. 13. 22:56class Solution:
def minCostConnectPoints(self, points: List[List[int]]) -> int:
def calc_Dist(p1, p2):
d1 = abs(p1[0] - p2[0])
d2 = abs(p1[1] - p2[1])
return d1 + d2
def find_parent(parent, a):
if parent[a] == a:
return a
parent[a] = find_parent(parent, parent[a])
return parent[a]
def union_parent(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
# (distance, pnt_idx1, pnt_idx2)
edges = []
for i in range(len(points)):
for j in range(i+1, len(points)):
pnt1 = points[i]
pnt2 = points[j]
edges.append((calc_Dist(pnt1, pnt2), i, j))
edges.sort()
parent = [0] * len(points)
for i in range(len(points)):
parent[i] = i
ret = 0
for edge in edges:
cost, i, j = edge
if find_parent(parent, i) != find_parent(parent, j):
union_parent(parent, i, j)
ret += cost
return ret
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 6. ZigZag Conversion (0) | 2021.08.16 |
---|---|
LeetCode: 207. Course Schedule (0) | 2021.08.14 |
LeetCode: 5. Longest Palindromic Substring (0) | 2021.08.12 |
LeetCode: 787. Cheapest Flights Within K Stops (0) | 2021.08.12 |
LeetCode: 3. Longest Substring Without Repeating Characters (0) | 2021.08.11 |