Concise Union-find(quick-find if more specific) in Python


  • 0
    A
    class Solution(object):
        def findCircleNum(self, M):
            """
            :type M: List[List[int]]
            :rtype: int
            """
            count = len(M)
            students = [i for i in xrange(len(M))]
    
            for i, row in enumerate(M):
                for j, column in enumerate(row):
                    if column == 1 and students[i] != students[j] and i != j:
                        count -= 1
                        tmp_i = students[i]
                        for k, student in enumerate(students):
                            if student == tmp_i:
                                students[k] = students[j]
    
            return count
    

Log in to reply
 

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