엔지니어 게시판
LeetCode 솔루션 분류

[2/12] 2477. 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. The capital city is city 0. You are given a 2D integer array roads where roads[i] = [ai, bi] denotes that there exists a bidirectional road connecting cities ai and bi.

There is a meeting for the representatives of each city. The meeting is in the capital city.

There is a car in each city. You are given an integer seats that indicates the number of seats in each car.

A representative can use the car in their city to travel or change the car and ride with another representative. The cost of traveling between two cities is one liter of fuel.

Return the minimum number of liters of fuel to reach the capital city.

 

Example 1:

Input: roads = [[0,1],[0,2],[0,3]], seats = 5
Output: 3
Explanation: 
- Representative1 goes directly to the capital with 1 liter of fuel.
- Representative2 goes directly to the capital with 1 liter of fuel.
- Representative3 goes directly to the capital with 1 liter of fuel.
It costs 3 liters of fuel at minimum. 
It can be proven that 3 is the minimum number of liters of fuel needed.

Example 2:

Input: roads = [[3,1],[3,2],[1,0],[0,4],[0,5],[4,6]], seats = 2
Output: 7
Explanation: 
- Representative2 goes directly to city 3 with 1 liter of fuel.
- Representative2 and representative3 go together to city 1 with 1 liter of fuel.
- Representative2 and representative3 go together to the capital with 1 liter of fuel.
- Representative1 goes directly to the capital with 1 liter of fuel.
- Representative5 goes directly to the capital with 1 liter of fuel.
- Representative6 goes directly to city 4 with 1 liter of fuel.
- Representative4 and representative6 go together to the capital with 1 liter of fuel.
It costs 7 liters of fuel at minimum. 
It can be proven that 7 is the minimum number of liters of fuel needed.

Example 3:

Input: roads = [], seats = 1
Output: 0
Explanation: No representatives need to travel to the capital city.

 

Constraints:

  • 1 <= n <= 105
  • roads.length == n - 1
  • roads[i].length == 2
  • 0 <= ai, bi < n
  • ai != bi
  • roads represents a valid tree.
  • 1 <= seats <= 105
태그

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
from collections import defaultdict, deque
import math
class Solution:
    def minimumFuelCost(self, roads: List[List[int]], seats: int) -> int:
        endpoints = set()
        graph = defaultdict(set)
        
        for v1, v2 in roads:
            graph[v1].add(v2)
            graph[v2].add(v1)

            if len(graph[v1]) == 1: endpoints.add(v1)
            elif v1 in endpoints: endpoints.remove(v1)
            if len(graph[v2]) == 1: endpoints.add(v2)
            elif v2 in endpoints: endpoints.remove(v2)

        people = {}

        dq = deque(list(endpoints))

        res = 0

        while dq:
            node = dq.popleft()
            if node == 0: continue
            if node not in people: people[node] = 1
            nxt = list(graph[node])[0]
            if nxt not in people: people[nxt] = 1
            del graph[node]
            graph[nxt].remove(node)

            people[nxt]+=people[node]
            res += math.ceil(people[node]/seats)
            del people[node]
            if len(graph[nxt]) == 1: dq.append(nxt)
        
        return res
전체 194 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 545 명
  • 오늘 방문자 4,251 명
  • 어제 방문자 7,102 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,547 명
알림 0