파비의 매일매일 공부기록

2023.03.22 Today's Challenge 본문

Problem Solving/LeetCode

2023.03.22 Today's Challenge

fabichoi 2023. 3. 22. 23:45

https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/

 

Minimum Score of a Path Between Two Cities - LeetCode

Can you solve this real interview question? Minimum Score of a Path Between Two Cities - You are given a positive integer n representing n cities numbered from 1 to n. You are also given a 2D array roads where roads[i] = [ai, bi, distancei] indicates that

leetcode.com

BFS로 풀면 되는 문제

class Solution:
    def minScore(self, n: int, roads: List[List[int]]) -> int:
        g = defaultdict(dict)
        for u, v, w in roads:
            g[u][v] = g[v][u] = w
        
        ms = float('inf')
        visited = set()
        qu = deque([1])

        while qu:
            node = qu.popleft()
            for adj, score in g[node].items():
                if adj not in visited:
                    qu.append(adj)
                    visited.add(adj)
                ms = min(ms, score)
        
        return ms
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.03.24 Today's Challenge  (0) 2023.03.24
2023.03.23 Today's Challenge  (0) 2023.03.23
2023.03.21 Today's Challenge  (0) 2023.03.21
2023.03.20 Today's Challenge  (0) 2023.03.20
2023.03.19 Today's Challenge  (0) 2023.03.19
Comments