Easy to understand C++ Solution, DFS, beats 93%
Runtime: 44 ms, faster than 93.45% of C++ online submissions for Pacific Atlantic Water Flow.\nMemory Usage: 14.1 MB, less than 100.00% of C++ online submission
Runtime: 44 ms, faster than 93.45% of C++ online submissions for Pacific Atlantic Water Flow.\nMemory Usage: 14.1 MB, less than 100.00% of C++ online submissions for Pacific Atlantic Water Flow.\n\n```\nclass Solution {\npublic:\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& matrix) {\n        \n        int n = matrix.size();\n        if(n == 0) return {};\n        int m = matrix[0].size();\n        \n        vector<vector<int>> res;\n        vector<vector<bool>> pacafic(n, vector<bool>(m, false));\n        vector<vector<bool>> atlantic(n, vector<bool>(m, false));\n        \n        for(int i=0; i<m; i++)\n        {\n            dfs(matrix, pacafic, INT_MIN, 0, i);\n            dfs(matrix, atlantic, INT_MIN, n-1, i);\n        }\n        \n        for(int i=0; i<n; i++)\n        {\n            dfs(matrix, pacafic, INT_MIN, i, 0);\n            dfs(matrix, atlantic, INT_MIN, i, m-1);\n        }\n        \n        for(int i=0; i<n; i++)\n        {\n            for(int j=0; j<m; j++)\n            {\n                if(pacafic[i][j] && atlantic[i][j])\n                    res.push_back(vector<int> {i,j});\n            }\n        }\n        \n        return res;\n    }\n    \n    void dfs(vector<vector<int>>& matrix, vector<vector<bool>>& visited, int prev, int i, int j)\n    {\n        if(i < 0 || j < 0 || i >= matrix.size() || j >= matrix[0].size() || prev > matrix[i][j] || visited[i][j]) return;\n        \n        visited[i][j] = true;\n        dfs(matrix, visited, matrix[i][j], i+1, j);\n        dfs(matrix, visited, matrix[i][j], i-1, j);\n        dfs(matrix, visited, matrix[i][j], i, j+1);\n        dfs(matrix, visited, matrix[i][j], i, j-1);\n    }\n};\n
['Depth-First Search']