[Python] Clean Union Find with explanation
Idea is simple.\n1. Iterate over the numbers, if you have seen it before it\'s a duplicate number so don\'t repeat the work. Actually, even if i repeated the wo
Idea is simple.\n1. Iterate over the numbers, if you have seen it before it\'s a duplicate number so don\'t repeat the work. Actually, even if i repeated the work it would still doesn\'t matter(except for penalty of redoing the work) but the algorithm would still be correct.\n2. Otherwise, find the adjacent element `(num+1, num-1)` \n3. Do union of adjacent elements and update the size of these nodes. I have modified the union method to return the updated size as it avoids me finding largest connected component once finished. Only time the max_size would change is when there is union of two nodes\n\n```\nclass Node:\n    def __init__(self, val):\n        self.val = val\n        self.parent = self\n        self.size = 1\n    \nclass UnionFind:\n        \n    def find(self, node):\n        if node.parent != node:\n            node.parent = self.find(node.parent)\n        return node.parent\n    \n    def union(self, node1, node2):\n        parent_1 = self.find(node1)\n        parent_2 = self.find(node2)\n        if parent_1 != parent_2:\n            parent_2.parent = parent_1\n            parent_1.size += parent_2.size\n        return parent_1.size\n                \n        \n        \nclass Solution:\n    def longestConsecutive(self, nums: List[int]) -> int:\n        uf = UnionFind()\n        nodes = {}\n        max_size = 0\n        for num in nums:\n            if num not in nodes:\n                node = Node(num)\n                nodes[num] = node\n                size = 1\n                if num + 1 in nodes:\n                    size = uf.union(node, nodes[num+1])\n                if num - 1 in nodes:\n                    size = uf.union(node, nodes[num-1])\n                max_size = max(max_size, size)\n                \n        return max_size\n\t\t```
['Union Find', 'Python', 'Python3']