일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 109. Convert Sorted List to Binary Search Tree
- t1
- 운영체제
- Decorator
- 시바견
- LeetCode
- attribute
- concurrency
- 밴픽
- 컴퓨터의 구조
- Convert Sorted List to Binary Search Tree
- Python Code
- Protocol
- data science
- shiba
- Class
- Regular Expression
- 715. Range Module
- Python
- 30. Substring with Concatenation of All Words
- 315. Count of Smaller Numbers After Self
- DWG
- iterator
- 프로그래머스
- Substring with Concatenation of All Words
- 파이썬
- kaggle
- Generator
- Python Implementation
- 43. Multiply Strings
Archives
- Today
- Total
Scribbling
LeetCode: 1466. Reorder Routes to Make All Paths Lead to the City Zero 본문
Computer Science/Coding Test
LeetCode: 1466. Reorder Routes to Make All Paths Lead to the City Zero
focalpoint 2021. 11. 23. 17:52DFS로 쉽게 풀 수 있다.
class Solution:
def minReorder(self, n: int, connections: List[List[int]]) -> int:
self.graph = [[] for _ in range(n)]
self.connections_set = set()
for u, v in connections:
self.graph[u].append(v)
self.graph[v].append(u)
self.connections_set.add((u, v))
visited = [False] * n
self.ret = 0
self.dfs(0, visited)
return self.ret
def dfs(self, node, visited):
visited[node] = True
for v in self.graph[node]:
if not visited[v]:
if (node, v) in self.connections_set:
self.ret += 1
self.dfs(v, visited)
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 238. Product of Array Except Self (0) | 2021.11.25 |
---|---|
LeetCode: 1329. Sort the Matrix Diagonally (0) | 2021.11.24 |
LeetCode: 149. Max Points on a Line (0) | 2021.11.22 |
LeetCode: 143. Reorder List (0) | 2021.11.22 |
LeetCode: 140. Word Break II (0) | 2021.11.22 |