일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- data science
- 315. Count of Smaller Numbers After Self
- Python Implementation
- Substring with Concatenation of All Words
- 43. Multiply Strings
- 밴픽
- Convert Sorted List to Binary Search Tree
- 시바견
- concurrency
- Python Code
- 715. Range Module
- DWG
- shiba
- 컴퓨터의 구조
- attribute
- 프로그래머스
- iterator
- Decorator
- kaggle
- Class
- Python
- Generator
- 30. Substring with Concatenation of All Words
- t1
- Regular Expression
- 운영체제
- 파이썬
- Protocol
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
Archives
- Today
- Total
Scribbling
LeetCode: 1101. The Earliest Moment When Everyone Become Friends 본문
Computer Science/Coding Test
LeetCode: 1101. The Earliest Moment When Everyone Become Friends
focalpoint 2023. 9. 14. 03:47https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/description/
class Solution:
def earliestAcq(self, logs: List[List[int]], n: int) -> int:
parents = [i for i in range(n)]
group_size = {i: 1 for i in range(n)}
def find_parent(a):
if parents[a] == a:
return a
parents[a] = find_parent(parents[a])
return parents[a]
def union(a, b):
a, b = find_parent(a), find_parent(b)
if a < b:
parents[b] = a
group_size[a] += group_size[b]
return group_size[a]
else:
parents[a] = b
group_size[b] += group_size[a]
return group_size[b]
logs.sort()
for t, u, v in logs:
if find_parent(u) != find_parent(v):
if union(u, v) == n:
return t
return -1
'Computer Science > Coding Test' 카테고리의 다른 글
[Programmers] 택배 배달과 수거하기 (0) | 2024.07.20 |
---|---|
Overlapping Intervals/Ranges (0) | 2023.04.24 |
[Dynamic Programming] Practice Question (0) | 2023.04.14 |
LeetCode: 2115. Find All Possible Recipes from Given Supplies (0) | 2022.05.03 |
LeetCode: 1032. Stream of Characters (0) | 2022.03.25 |