当前位置:网站首页>LeetCode Algorithm 1791. Find the central node of the star chart

LeetCode Algorithm 1791. Find the central node of the star chart

2022-06-12 01:42:00 Alex_ 12 hours a day 6 days a week

Topic link :1791. Find the central node of the star graph

Ideas

Algorithm : Count
data structure : chart
Ideas : The central node is actually a degree of n-1 The node of , So we can use a counter to count the degrees of all nodes , If the degree is n-1, Then it is the central node . There is no given... In the input of the title n, So I went to find this by myself , And the title gives a two-dimensional array , It's not easy to operate , It is best to flatten it into a one-dimensional array , Reference article : Will be irregular Python The multidimensional array is flattened to one dimension .

Code

Python

class Solution:
    def findCenter(self, edges: List[List[int]]) -> int:
        from itertools import chain
        from collections import Counter

        array = list(chain(*edges))
        n = max(array)
        cnt = Counter(array)
        for key, val in cnt.items():
            if val == n - 1:
                return key
原网站

版权声明
本文为[Alex_ 12 hours a day 6 days a week]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120137090480.html