일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Daily Challenge
- 읽기
- 화상영어
- 파비최
- 만화도
- 리얼 클래스
- 사이드
- 10분
- 쓰릴오브파이트
- 스탭퍼
- 영어원서읽기
- 운동
- 괜찮음
- 프로젝트
- 링피트
- Writing
- English
- 3줄정리
- 개발자
- 매일
- Problem Solving
- leetcode
- 미드시청
- FIT XR
- realclass
- 30분
- 영어공부
- 잡생각
- 월간
- 뭐든
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.03.23 Today's Challenge 본문
https://leetcode.com/problems/number-of-operations-to-make-network-connected/
Number of Operations to Make Network Connected - LeetCode
Can you solve this real interview question? Number of Operations to Make Network Connected - There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection b
leetcode.com
오늘은 DFS로 푸는 문제!
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
if len(connections) < n - 1:
return -1
g = [set() for i in range(n)]
for u, v in connections:
g[u].add(v)
g[v].add(u)
visited = [0] * n
def dfs(node):
if visited[node]:
return 0
visited[node] = 1
for neighbor in g[node]:
dfs(neighbor)
return 1
return sum(dfs(node) for node in range(n)) - 1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.03.25 Today's Challenge (0) | 2023.03.25 |
---|---|
2023.03.24 Today's Challenge (0) | 2023.03.24 |
2023.03.22 Today's Challenge (0) | 2023.03.22 |
2023.03.21 Today's Challenge (0) | 2023.03.21 |
2023.03.20 Today's Challenge (0) | 2023.03.20 |
Comments