Back to course home
0% completed
Vote For New Content
More fun version (to me anyway)
Lee
Mar 6, 2024
def findDistinctIslandsBit(self, matrix): if not matrix or not matrix[0]: return None # iterate over the matrix finding islands # dfs the island shifting the islands "code" based on the traversal # add the code int to a set, then return the size of the set # will use explicit stack dfs (for no particular reason) def dfsIsland(rowStart, colStart): code = 0 stack = [(rowStart, colStart)] while stack: i, j = stack.pop() code <<= 1 if not (0 <= i < n) or not (0 <= j < m): continue code += matrix[i][j] if matrix[i][j]: for diff in [-1, 1]: stack.append((i + diff, j)) stack.append((i, j + diff)) matrix[i][j] = 0 return code n = len(matrix) m = len(matrix[0]) islands = set() for i in range(n): for j in range(m): if matrix[i][j]: islandCode = dfsIsland(i, j) islands.add(islandCode) return len(islands)
0
0
Comments
Comments
On this page