|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +/* |
| 5 | + * @lc app=leetcode id=417 lang=java |
| 6 | + * |
| 7 | + * [417] Pacific Atlantic Water Flow |
| 8 | + * |
| 9 | + * https://leetcode.com/problems/pacific-atlantic-water-flow/description/ |
| 10 | + * |
| 11 | + * algorithms |
| 12 | + * Medium (42.38%) |
| 13 | + * Total Accepted: 98.2K |
| 14 | + * Total Submissions: 231K |
| 15 | + * Testcase Example: '[[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]]' |
| 16 | + * |
| 17 | + * Given an m x n matrix of non-negative integers representing the height of |
| 18 | + * each unit cell in a continent, the "Pacific ocean" touches the left and top |
| 19 | + * edges of the matrix and the "Atlantic ocean" touches the right and bottom |
| 20 | + * edges. |
| 21 | + * |
| 22 | + * Water can only flow in four directions (up, down, left, or right) from a |
| 23 | + * cell to another one with height equal or lower. |
| 24 | + * |
| 25 | + * Find the list of grid coordinates where water can flow to both the Pacific |
| 26 | + * and Atlantic ocean. |
| 27 | + * |
| 28 | + * Note: |
| 29 | + * |
| 30 | + * |
| 31 | + * The order of returned grid coordinates does not matter. |
| 32 | + * Both m and n are less than 150. |
| 33 | + * |
| 34 | + * |
| 35 | + * |
| 36 | + * |
| 37 | + * Example: |
| 38 | + * |
| 39 | + * |
| 40 | + * Given the following 5x5 matrix: |
| 41 | + * |
| 42 | + * Pacific ~ ~ ~ ~ ~ |
| 43 | + * ~ 1 2 2 3 (5) * |
| 44 | + * ~ 3 2 3 (4) (4) * |
| 45 | + * ~ 2 4 (5) 3 1 * |
| 46 | + * ~ (6) (7) 1 4 5 * |
| 47 | + * ~ (5) 1 1 2 4 * |
| 48 | + * * * * * * Atlantic |
| 49 | + * |
| 50 | + * Return: |
| 51 | + * |
| 52 | + * [[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (positions with |
| 53 | + * parentheses in above matrix). |
| 54 | + * |
| 55 | + * |
| 56 | + * |
| 57 | + * |
| 58 | + */ |
| 59 | +class Solution { |
| 60 | + |
| 61 | + public List<List<Integer>> pacificAtlantic(int[][] matrix) { |
| 62 | + if (matrix.length == 0) { |
| 63 | + return new ArrayList<List<Integer>>(); |
| 64 | + } |
| 65 | + |
| 66 | + int rows = matrix.length, cols = matrix[0].length; |
| 67 | + |
| 68 | + int[][] atlantic = new int[rows][cols]; |
| 69 | + int[][] pacific = new int[rows][cols]; |
| 70 | + |
| 71 | + // Top bottom |
| 72 | + for (int col = 0; col < cols; ++col) { |
| 73 | + dfs(matrix, 0, col, Integer.MIN_VALUE, pacific); |
| 74 | + dfs(matrix, rows - 1, col, Integer.MIN_VALUE, atlantic); |
| 75 | + } |
| 76 | + |
| 77 | + // left right |
| 78 | + for (int row = 0; row < rows; ++row) { |
| 79 | + dfs(matrix, row, 0, Integer.MIN_VALUE, pacific); |
| 80 | + dfs(matrix, row, cols - 1, Integer.MIN_VALUE, atlantic); |
| 81 | + } |
| 82 | + |
| 83 | + ArrayList<List<Integer>> res = new ArrayList<List<Integer>>(); |
| 84 | + |
| 85 | + for (int row = 0; row < rows; ++row) { |
| 86 | + for (int col = 0; col < cols; ++col) { |
| 87 | + if (pacific[row][col] == 1 && atlantic[row][col] == 1) { |
| 88 | + LinkedList<Integer> coords = new LinkedList<Integer>(); |
| 89 | + coords.add(row); |
| 90 | + coords.add(col); |
| 91 | + res.add(coords); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return res; |
| 97 | + } |
| 98 | + |
| 99 | + public void dfs(int[][] matrix, int row, int col, int lastVal, int[][] ocean) { |
| 100 | + if (row < 0 || col < 0 || row >= matrix.length || col >= matrix[0].length) { |
| 101 | + return; |
| 102 | + } |
| 103 | + if (matrix[row][col] < lastVal) { |
| 104 | + return; |
| 105 | + } |
| 106 | + if (ocean[row][col] == 1) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + ocean[row][col] = 1; |
| 111 | + |
| 112 | + dfs(matrix, row + 1, col, matrix[row][col], ocean); |
| 113 | + dfs(matrix, row - 1, col, matrix[row][col], ocean); |
| 114 | + dfs(matrix, row, col + 1, matrix[row][col], ocean); |
| 115 | + dfs(matrix, row, col - 1, matrix[row][col], ocean); |
| 116 | + } |
| 117 | +} |
0 commit comments