Python solution using sink


  • 0
    E

    Similar to the island problem, we sink all the numbers that we found so far.

    class Solution(object):
        def findCircleNum(self, M):
            """
            :type M: List[List[int]]
            :rtype: int
            """
            m=len(M)
            count=0
        
            def sink(i):
                M[i][i]=0
                for j in range(m):
                    if M[i][j]==1 and M[j][j]==1:
                        sink(j)
                        
            for i in range(m):
                if M[i][i]==1:
                    sink(i)
                    count+=1
            return count
            
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.