Clean DFS solution with minimum number of recursive calls in C++
Time Complexity = O(N * M)\nSpace Complexity = O(N * M)\n\n\nclass Solution {\npublic:\n/*\n    TC: O(N*M)\n    SC: O(N*M)\n*/\n    \n    vector<int> dx = {-1, 
Time Complexity = O(N * M)\nSpace Complexity = O(N * M)\n\n```\nclass Solution {\npublic:\n/*\n    TC: O(N*M)\n    SC: O(N*M)\n*/\n    \n    vector<int> dx = {-1, 1, 0, 0};\n    vector<int> dy = {0, 0, 1, -1};\n    \n    bool valid(int i, int j, int n, int m){\n        return i >= 0 && i < n && j >= 0 && j < m;\n    }\n    \n    void DFS(vector<vector<int>>& heights, int i, int j, int prev,vector<vector<bool>>& anyOcean){\n        if(heights[i][j] < prev || anyOcean[i][j]) return;\n        anyOcean[i][j] = true;\n        for(int k = 0; k < 4; k++){\n            int row = dx[k] + i;\n            int col = dy[k] + j;\n            if(valid(row, col, heights.size(), heights[0].size()) && !anyOcean[row][col])\n                DFS(heights, row, col, heights[i][j], anyOcean);\n        }\n    }\n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {\n        int n = heights.size(), m = heights[0].size();\n        vector<vector<int>> res;\n        vector<vector<bool>> atlantic(n, vector<bool>(m)), pacific(n, vector<bool>(m));\n        \n        // check for columns\n        for(int i = 0; i < m; i++){\n            DFS(heights, 0, i, INT_MIN, pacific);\n            DFS(heights, n-1, i, INT_MIN, atlantic);\n        }\n        \n        // check for rows\n        for(int i = 0; i < n; i++){\n            DFS(heights, i, 0, INT_MIN, pacific);\n            DFS(heights, i, m-1, INT_MIN, atlantic);\n        }\n        \n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < m; j++){\n                if(atlantic[i][j] && pacific[i][j])\n                    res.push_back({i, j});\n            }\n        }\n        return res;\n    }\n};\n```
['Depth-First Search', 'Graph', 'C']