GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — algorithms/dfs/pacific_atlantic.py at master · deba999/algorithms Expand file tree/ Copy pathpacific_atlantic.pyMore file actions 66 lines (56 loc) · 2 KB / Copy pathpacific_atlantic.pyFile metadata and controls66 lines (56 loc) · 2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 # Given an m x n matrix of non-negative integers representing # the height of each unit cell in a continent, # the "Pacific ocean" touches the left and top edges of the matrix # and the "Atlantic ocean" touches the right and bottom edges. # Water can only flow in four directions (up, down, left, or right) # from a cell to another one with height equal or lower. # Find the list of grid coordinates where water can flow to both the # Pacific and Atlantic ocean. # Note: # The order of returned grid coordinates does not matter. # Both m and n are less than 150. # Example: # Given the following 5x5 matrix: # Pacific ~ ~ ~ ~ ~ # ~ 1 2 2 3 (5) * # ~ 3 2 3 (4) (4) * # ~ 2 4 (5) 3 1 * # ~ (6) (7) 1 4 5 * # ~ (5) 1 1 2 4 * # * * * * * Atlantic # Return: # [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] # (positions with parentheses in above matrix). def pacific_atlantic(matrix): """ :type matrix: List[List[int]] :rtype: List[List[int]] """ n = len(matrix) if not n: return [] m = len(matrix[0]) if not m: return [] res = [] atlantic = [[False for _ in range(n)] for _ in range(m)] pacific = [[False for _ in range(n)] for _ in range(m)] for i in range(n): DFS(pacific, matrix, float("-inf"), i, 0) DFS(atlantic, matrix, float("-inf"), i, m - 1) for i in range(m): DFS(pacific, matrix, float("-inf"), 0, i) DFS(atlantic, matrix, float("-inf"), n - 1, i) for i in range(n): for j in range(m): if pacific[i][j] and atlantic[i][j]: res.append([i, j]) return res def DFS(grid, matrix, height, i, j): if i < 0 or i >= len(matrix) or j < 0 or j >= len(matrix[0]): return if grid[i][j] or matrix[i][j] < height: return grid[i][j] = True DFS(grid, matrix, matrix[i][j], i - 1, j) DFS(grid, matrix, matrix[i][j], i + 1, j) DFS(grid, matrix, matrix[i][j], i, j - 1) DFS(grid, matrix, matrix[i][j], i, j + 1) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |