Find Center of Star Graph LeetCode solution in Python

1
class Solution:
2
def findCenter(self, edges: List[List[int]]) -> int:
3
"""From the Constraints: A valid STAR GRAPH is confirmed.
4
That means the center will be common to every edges.
5
Therefore we can get the center by comparing only first 2 elements"""
6

7
for i in range(1):
8

9
# Check if first element of first edge mathches with any element of second edges
10

11
if edges[i][0] == edges[i + 1][0] or edges[i][0] == edges[i + 1][1]:
12
return edges[i][0]
13

14
# Otherwise second element of first edge will be the answer
15
else:
16
return edges[i][1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0