200 Number of Islands 34.8% Medium 128 Longest Consecutive Sequence 37.0% Hard 130 Surrounded Regions 18.6% Medium 547 Friend Circles 49.1% Medium 305 Number of Islands II 39.0% Hard 261 Graph Valid Tree 37.9% Medium 323 Number of Connected Components in an Undirected Graph
T O(n) union-find nearly o(1) S O(n) classSolution(object): defvalidTree(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: bool """ group = [i for i inrange(n)] for e1, e2 in edges: root1 = self.find(e1, group) root2 = self.find(e2, group) if root1 == root2: returnFalse else: group[root2] = root1 returnlen(edges) == n - 1 deffind(self, e, group): if e == group[e]: return e else: returnself.find(group[e], group)
323. Number of Connected Components in an Undirected Graph
T O(n) union-find nearly o(1) S O(n) classSolution(object): defcountComponents(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: int """ count = n group = [i for i inrange(n)] for e1, e2 in edges: root1 = self.find(e1, group) root2 = self.find(e2, group) if root1 == root2: pass else: count -= 1 group[root2] = root1 return count deffind(self, e, group): if e == group[e]: return e else: returnself.find(group[e], group)
547. Friend Circles
1 2 3 4 5 6 7
Input: [[1,1,0], [1,1,1], [0,1,1]] Output: 1 Explanation:The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends, so the 0th and 2nd students are indirect friends. All of them are in the same friend circle, so return 1.
这道题和上一道题类似,算出孤立的朋友就好… trick的地方就是我们只用算一半的矩阵就好,因为If M[i][j] = 1, then M[j][i] = 1. 而且对角线一定为1
T O(n^2) union-find nearly o(1) S O(n) classSolution(object): deffindCircleNum(self, M): """ :type M: List[List[int]] :rtype: int """ ifnot M ornot M[0]: return0 m = len(M) group = [i for i inrange(m)] count = m # init for i inrange(m): for j inrange(i+1,m): if M[i][j] == 1: p1 = self.find(i, group) p2 = self.find(j, group) if p1 != p2: count -= 1 group[p2] = p1 return count deffind(self, e, group): if e == group[e]: return e else: returnself.find(group[e], group)
classSolution(object): defnumIslands(self, grid): """ :type grid: List[List[str]] :rtype: int """ # TIME O(MN) # SPACE O(MN) ifnot grid ornotlen(grid) ornot grid[0]: return0 m = len(grid) n = len(grid[0]) # two dimension to one groupTag = [0for i inrange(m*n)]
for i inrange(m): for j inrange(n): if grid[i][j] == '1': groupTag[i*n+j] = i*n + j else: groupTag[i*n+j] = -1 for i inrange(m): for j inrange(n): if grid[i][j] == '0': continue if j+1 < n and grid[i][j+1] == '1': self.union(i,j,i,j+1,groupTag,n) if i+1 < m and grid[i+1][j] == '1': self.union(i,j,i+1,j,groupTag,n) count = 0 for i inrange(len(groupTag)): if groupTag[i] == i: count += 1 return count deffind(self, e, groupTag): # isolate if groupTag[e] == e: return e # group else: returnself.find(groupTag[e], groupTag) defunion(self, i, j, x, y, groupTag, n): index1 = i*n+j index2 = x*n+y root1 = self.find(index1, groupTag) root2 = self.find(index2, groupTag) # already unioned if root1 == root2: return else: groupTag[root2] = root1
classSolution(object): defnumIslands2(self, m, n, positions): ans = [] islands = Union() for p inmap(tuple, positions): islands.add(p) for dp in (0, 1), (0, -1), (1, 0), (-1, 0): q = (p[0] + dp[0], p[1] + dp[1]) if q in islands.group: islands.union(p, q) ans += [islands.count] return ans