Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Pacific Atlantic Water Flow problem solution

YASH PAL, 31 July 2024

In this Leetcode Pacific Atlantic Water Flow problem solution, There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island’s left and top edges, and the Atlantic Ocean touches the island’s right and bottom edges.

The island is partitioned into a grid of square cells. You are given an m x n integer matrix heights where heights[r][c] represents the height above sea level of the cell at coordinate (r, c).

The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell’s height is less than or equal to the current cell’s height. Water can flow from any cell adjacent to an ocean into the ocean.

Return a 2D list of grid coordinates result where result[i] = [ri, ci] denotes that rain water can flow from cell (ri, ci) to both the Pacific and Atlantic oceans.

leetcode pacific atlantic water flow problem solution

Problem solution in Python.

class Solution:
    def pacificAtlantic(self, matrix: List[List[int]]) -> List[List[int]]:
        if not matrix: return []
        row = len(matrix)
        col = len(matrix[0])
        pacific = [["0" for _ in range(col)] for _ in  range(row)]
        atlantic = [["0" for _ in range(col)] for _ in  range(row)]
        def function(r,c,sea,val,M) :
            if 0<=r<row and 0<=c<col and M[r][c]  == "0" :
                if matrix[r][c] >= val : 
                    #print(r,c)
                    M[r][c] = sea
                    function(r-1,c,sea,matrix[r][c],M) 
                    function(r+1,c,sea,matrix[r][c],M) 
                    function(r,c-1,sea,matrix[r][c],M) 
                    function(r,c+1,sea,matrix[r][c],M) 
                
        for i in range(col) :
            function(0,i,"P",-1,pacific)
        for i in range(row) :
            function(i,0,"P",-1,pacific)
        for i in range(col-1,-1,-1) :
            function(row-1,i,"A",-1,atlantic)
        for i in range(row-1,-1,-1) :
            function(i,col-1,"A",-1,atlantic)
        answer = []
        
        for i in range(row) :
            for j in range(col) :
                if atlantic[i][j] == "A" and pacific[i][j] == "P" :
                    answer.append([i,j])
                    
        return answer

Problem solution in Java.

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public int[][] direction = {{1,0},{-1,0},{0,1},{0,-1}};
    public List<List<Integer>> pacificAtlantic(int[][] matrix) {
        //corner case
        if(matrix==null || matrix.length==0 || matrix[0].length==0) return res;
        int m=matrix.length;
        int n=matrix[0].length;
        
        
        boolean[][] pacific = new boolean[m][n];
        boolean[][] atlantic = new boolean[m][n];
        for(int i=0;i<m;i++){
            dfs(matrix,i,0,pacific,matrix[i][0]);
            dfs(matrix,i,n-1,atlantic,matrix[i][n-1]);
        }
        for(int j=0;j<n;j++){
            dfs(matrix,0,j,pacific,matrix[0][j]);
            dfs(matrix,m-1,j,atlantic,matrix[m-1][j]);
        }
        
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(pacific[i][j]==true && atlantic[i][j]==true){
                    List<Integer> temp=new ArrayList<>();
                    temp.add(i);
                    temp.add(j);
                    res.add(temp);
                }
            }
        }
        return res;
    }
    
    private void dfs(int[][] matrix,int r,int c,boolean[][] isTrue,int val){
        int m=matrix.length;
        int n=matrix[0].length;
        
        if(r<0 || r>=m || c<0 || c>=n || matrix[r][c]<val) return;
        if(isTrue[r][c]) return;
        
        isTrue[r][c]=true;
        
        for(int i=0;i<direction.length;i++){
            dfs(matrix,r+direction[i][0],c+direction[i][1],isTrue,matrix[r][c]);
        }
        
    }
}

Problem solution in C++.

class Solution {
public:
    int m, n;
    void dfs(vector<vector<int>>& matrix, vector<vector<bool>>& vis, int i, int j)
    {
        vis[i][j] = true;
        int X[] = {1, 0, -1, 0};
        int Y[] = {0, 1, 0, -1};
        for(int k = 0; k < 4; k++)
        {
            int x = i + X[k];
            int y = j + Y[k];
            if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && matrix[x][y] >= matrix[i][j] )
            {
                vis[x][y] = true;
                dfs(matrix, vis, x, y);
            }
        }
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {
        vector<vector<int>>ans;
        m = matrix.size();
        if(m == 0)return ans;
        n = matrix[0].size();
        vector<vector<bool>>p(m, vector<bool>(n, false)), a(m, vector<bool>(n, false));
        for(int i = 0; i < m; i++)
        {
            if(!p[i][0])
                dfs(matrix, p, i, 0);
            if(!a[i][n - 1])
                dfs(matrix, a, i, n - 1);
        }
        for(int i = 0; i < n; i++)
        {
            if(!p[0][i])
                dfs(matrix, p, 0, i);
            if(!a[m - 1][i])
                dfs(matrix, a, m - 1, i);
        }
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(p[i][j] && a[i][j])
                    ans.push_back({i, j});
            }
        }
        return ans;
    }
};

Problem solution in C.

void dfs(int** matrix, int matrixRowSize, int *matrixColSizes,int** map,int row,int col,int value,int** ret,int* returnSize){
    if(map[row][col]==value){
        return;
    }
    int direction[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    if(map[row][col]==0){
        map[row][col]=value;
    }else{
        map[row][col]=value;
        ret[(*returnSize)++]=(int*)calloc(2,sizeof(int));
        ret[(*returnSize)-1][0]=row;
        ret[(*returnSize)-1][1]=col;
    }
    for(int i=0;i<4;i++){
        int tmp_row=row+direction[i][0];
        int tmp_col=col+direction[i][1];
        if(tmp_row>-1&&tmp_row<matrixRowSize&&tmp_col>-1
           &&tmp_col<matrixColSizes[tmp_row]&&matrix[row][col]<=matrix[tmp_row][tmp_col]){
            dfs(matrix,matrixRowSize,matrixColSizes,map,tmp_row,tmp_col,value,ret,returnSize);
        }
    }
}
int** pacificAtlantic(int** matrix, int matrixRowSize, int *matrixColSizes, int** columnSizes, int* returnSize) {
    int** map=(int**)malloc(matrixRowSize*sizeof(int*));
    *returnSize=0;
    if(matrixRowSize==0){
        return NULL;
    }
    columnSizes[0]=(int*)malloc(matrixRowSize*matrixColSizes[0]*sizeof(int));
    int** ret=(int**)malloc(matrixRowSize*matrixColSizes[0]*sizeof(int*));
    for(int i=0;i<matrixRowSize;i++){
        map[i]=(int*)calloc(matrixColSizes[i],sizeof(int));
    }
    for(int i=0;i<matrixColSizes[0];i++){
        dfs(matrix,matrixRowSize,matrixColSizes,map,0,i,1,ret,returnSize);
    }
    for(int i=0;i<matrixRowSize;i++){
        dfs(matrix,matrixRowSize,matrixColSizes,map,i,0,1,ret,returnSize);
    }
    for(int i=0;i<matrixColSizes[0];i++){
        dfs(matrix,matrixRowSize,matrixColSizes,map,matrixRowSize-1,i,-1,ret,returnSize);
    }
    for(int i=0;i<matrixRowSize;i++){
        dfs(matrix,matrixRowSize,matrixColSizes,map,i,matrixColSizes[0]-1,-1,ret,returnSize);
    }
    for(int i=0;i<*returnSize;i++){
        columnSizes[0][i]=2;
    }
    return ret;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes