일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- leetcode
- Writing
- Daily Challenge
- 3줄정리
- 리얼 클래스
- 링피트
- 프로젝트
- 영어공부
- 30분
- 미드시청
- 뭐든
- 10분
- 파비최
- 개발자
- 잡생각
- 영어원서읽기
- FIT XR
- realclass
- 사이드
- 쓰릴오브파이트
- Problem Solving
- 화상영어
- 매일
- 괜찮음
- 월간
- 스탭퍼
- 만화도
- English
- 읽기
- 운동
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.03.24 Today's Challenge 본문
https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/
Reorder Routes to Make All Paths Lead to the City Zero - LeetCode
Can you solve this real interview question? Reorder Routes to Make All Paths Lead to the City Zero - There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tre
leetcode.com
DFS로 푸는 문제!
class Solution:
def minReorder(self, n: int, connections: List[List[int]]) -> int:
self.res = 0
roads = set()
g = collections.defaultdict(list)
for u, v in connections:
roads.add((u,v))
g[v].append(u)
g[u].append(v)
def dfs(u, parent):
self.res += (parent, u) in roads
for v in g[u]:
if v == parent:
continue
dfs(v, u)
dfs(0, -1)
return self.res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.03.26 Today's Challenge (0) | 2023.03.26 |
---|---|
2023.03.25 Today's Challenge (0) | 2023.03.25 |
2023.03.23 Today's Challenge (0) | 2023.03.23 |
2023.03.22 Today's Challenge (0) | 2023.03.22 |
2023.03.21 Today's Challenge (0) | 2023.03.21 |
Comments