일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 리얼 클래스
- 링피트
- 프로젝트
- 괜찮음
- 영어공부
- 3줄정리
- 뭐든
- 사이드
- 쓰릴오브파이트
- Writing
- 매일
- 읽기
- 잡생각
- realclass
- 화상영어
- Daily Challenge
- 미드시청
- leetcode
- 파비최
- English
- 영어원서읽기
- Problem Solving
- FIT XR
- 스탭퍼
- 월간
- 운동
- 30분
- 만화도
- 개발자
- 10분
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.12 Today's Challenge 본문
https://leetcode.com/problems/minimum-fuel-cost-to-report-to-the-capital/
Minimum Fuel Cost to Report to the Capital - LeetCode
Can you solve this real interview question? Minimum Fuel Cost to Report to the Capital - There is a tree (i.e., a connected, undirected graph with no cycles) structure country network consisting of n cities numbered from 0 to n - 1 and exactly n - 1 roads.
leetcode.com
오늘은 DFS 문제!
class Solution:
def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
ans = 0
graph = [[] for _ in range(len(roads) + 1)]
for u, v in roads:
graph[u].append(v)
graph[v].append(u)
def dfs(u, prev):
nonlocal ans
people = 1
for v in graph[u]:
if v == prev:
continue
people += dfs(v, u)
if u > 0:
ans += int(math.ceil(people/seats))
return people
dfs(0, -1)
return ans
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.02.14 Today's Challenge (0) | 2023.02.14 |
---|---|
2023.02.13 Today's Challenge (0) | 2023.02.13 |
2023.02.11 Today's Challenge (0) | 2023.02.11 |
2023.02.10 Today's Challenge (0) | 2023.02.10 |
2023.02.09 Today's Challenge (0) | 2023.02.09 |
Comments