shriyanshi24jindal

Pacific Atlantic Water Flow

Mar 29th, 2026
142
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
Java 16.86 KB | None | 0 0
  1. class Solution {
  2. /**
  3.   * Key Observation: Water can flow from cell A → B if: height[B] ≤ height[A]
  4.   * Instead of simulating water from every cell to the ocean (very expensive), we reverse the flow:
  5.   * We move from ocean → inside and only go to cells with equal or greater height.
  6.   height[next] >= height[current] - This tells us which cells can reach the ocean.
  7.   * We maintain two visited matrices:
  8.   pacific[n][m] -> cells that can reach Pacific
  9.   atlantic[n][m] -> cells that can reach Atlantic
  10.   * Start DFS From Ocean Borders
  11.   Pacific Ocean touches:
  12.   Top row
  13.   Left column
  14.   So run DFS from: (i,0) for all rows (0,j) for all columns
  15.   Mark all reachable cells in pacific.
  16.   Atlantic Ocean touches:
  17.   Last row
  18.   Right column
  19.   So run DFS from: (n-1,j) for all rows (i,m-1) for all columns
  20.   Mark all reachable cells in Atlantic.
  21.   * DFS moves to neighbors only if height is increasing or equal.
  22.   Condition preventing DFS:
  23.   1. Out of bounds
  24.   2. Already visited
  25.   3. Next height < previous height
  26.   * Explore All 4 Directions
  27.   From every cell we visit:
  28.   down -> (i+1, j)
  29.   right -> (i, j+1)
  30.   left -> (i, j-1)
  31.   up -> (i-1, j)
  32.  
  33.   Find Cells That Reach Both Oceans
  34.   Finally iterate the grid:
  35.   if(pacific[i][j] && atlantic[i][j])
  36.   add to result
  37.   */
  38. public List<List<Integer>> pacificAtlantic(int[][] heights) {
  39. List<List<Integer>> res = new ArrayList<>();
  40.  
  41. int n = heights.length;
  42. int m = heights[0].length;
  43. boolean[][] pacific = new boolean[n][m];
  44. boolean[][] atlantic = new boolean[n][m];
  45.  
  46. for(int i=0; i<n ;i++)
  47. {
  48. // 0th column
  49. // -1 and -1 represent the old ith and jth cell
  50. markCells(heights, pacific, i, 0, -1, -1, n, m);
  51.  
  52. // last column
  53. // -1 and -1 represent the old ith and jth cell
  54. markCells(heights, atlantic, i, m-1, -1, -1, n, m);
  55. }
  56.  
  57. for(int j=0; j<m ;j++)
  58. {
  59. // 0th row
  60. // -1 and -1 represent the old ith and jth cell
  61. markCells(heights, pacific, 0, j, -1, -1, n, m);
  62.  
  63. // last row
  64. // -1 and -1 represent the old ith and jth cell
  65. markCells(heights, atlantic, n-1, j, -1, -1, n, m);
  66. }
  67.  
  68. for(int i=0; i<n; i++)
  69. {
  70. for(int j=0; j<m; j++)
  71. {
  72. if(pacific[i][j] == true && atlantic[i][j] == true)
  73. {
  74. List<Integer> temp = new ArrayList<>();
  75. temp.add(i);
  76. temp.add(j);
  77. if(!res.contains(temp))
  78. res.add(temp);
  79. }
  80. }
  81. }
  82. return res;
  83. }
  84.  
  85. public void markCells(int[][] heights, boolean[][] visited, int i, int j, int oldi, int oldj, int n, int m)
  86. {
  87. // if the current height is less than or equal to the old cell's height then water can't flow
  88. if(i < 0 || j < 0 || i >= n || j >= m || visited[i][j] == true ||
  89. (oldi >=0 && oldj >=0 && heights[oldi][oldj] > heights[i][j]))
  90. return;
  91.  
  92. visited[i][j] = true;
  93. // visiting in all 4-directions
  94. markCells(heights, visited, i+1, j, i, j, n, m);
  95. markCells(heights, visited, i, j+1, i, j, n, m);
  96. markCells(heights, visited, i, j-1, i, j, n, m);
  97. markCells(heights, visited, i-1, j, i, j, n, m);
  98. }
  99. }
RAW Paste Data Copied