무냐의 개발일지

1791. Find Center of Star GraphS 본문

LeetCode 코딩테스트

1791. Find Center of Star GraphS

무냐코드 2024. 5. 9. 16:48

👩‍💻 문제

There is an undirected star graph consisting of n nodes labeled from 1 to n. A star graph is a graph where there is one center node and exactly n - 1 edges that connect the center node with every other node.
You are given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi. Return the center of the given star graph.

💡 풀이

class Solution:
    def findCenter(self, edges: List[List[int]]) -> int:
        if edges[0][0] in edges[1]:
            return edges[0][0]
        else:
            return edges[0][1]

 

✍️ 해설

easy. 심플한 원리를 이용했다.
무조건 모든 노드에 공통적으로 들어가는 숫자일테니, 0, 1번 노드에 공통으로 들어가는 숫자가 바로 center이다