무냐의 개발일지
1791. Find Center of Star GraphS 본문
👩💻 문제
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이다
'LeetCode 코딩테스트' 카테고리의 다른 글
LL: Reverse Between 해설 (0) | 2024.06.14 |
---|---|
627. Swap Salary (0) | 2024.05.09 |
1920. Build Array from Permutation (0) | 2024.04.15 |
2859. Sum of Values at Indices With K Set Bits (0) | 2024.04.14 |
2114. Maximum Number of Words Found in Sentences (0) | 2024.04.13 |