🥇 C++  ||  EXPLAINED  ||  ; ]
UPVOTE IF HELPFuuL\n\nWe need to count the number of cells from which water could flow to both the oceans.\nWater can go to left,top for pacific and to right,do
**UPVOTE IF HELPFuuL**\n\nWe need to count the number of cells from which water could flow to both the oceans.\nWater can go to **left,top** for pacific and to **right,down** for atlantic.\n\n**APPROACH**\n\nWe just do what is asked for.\n\nWhen at cell ```ht[i][j]``` :\n* we check that water can flow to **left,top** and also to **right,down**.\n* It is similar to finding a path with non-increasing values.\n* To prevent repeation, we use memoization to store the result for certain ```ht[i][j]```.\n\n*How memoization helps :*\n* Let water can flow to both oceans from cell ```ht[i][j]```, so while checking for its adjacent cells, we need not find the complete path to oceans, **we just find a path that leads to a cell that reaches both oceans.**\n\n**Finding water can reach both oceans :**\n* As we going for memoization, we create two ```vector<vector<bool>>``` that stores that whether water can flow to ocean for each ocean respectively.\n* For pacific, we created ```pac``` and water initially water can flow for top and left cells.\n* For atlanic, we created ```atl``` and water initially water can flow for bottom and right cells.\n* Then for each cell, we check whether water flows off :\n* * For cell ```ht[i][j]``` we recurcively call for its adjacent cells, if neightbouring height is less or equal to current cell.\n* * Water can flow off if we reach a cell for which we already have solution.\n* * * For pacific we already made top,left cells ```true``` and same for atlantic.\n\n* Whenever water reaches, make all cells in that path ```true``` so they need not to be calculated in future.\n\n\n\n**UPVOTE IF HELPFuuL**\n\n**C++**\n```\nclass Solution {\npublic:\n    int m,n;\n    \n    bool s(vector<vector<bool>>& ocean, int i, int j, vector<vector<int>>& ht){\n        \n        if (i<0 || j<0 || i==m || j==n || ht[i][j]==100004) return false;\n        if (ocean[i][j]) return true;\n        \n        int k = ht[i][j];\n        ht[i][j]=100004;\n        bool zz = false;\n        if (i>0 && ht[i-1][j]<=k)   zz = zz || s(ocean,i-1,j,ht);\n        if (j>0 && ht[i][j-1]<=k)   zz = zz || s(ocean,i,j-1,ht);\n        if (i<m-1 && ht[i+1][j]<=k) zz = zz || s(ocean,i+1,j,ht);\n        if (j<n-1 && ht[i][j+1]<=k) zz = zz || s(ocean,i,j+1,ht);\n        \n        ocean[i][j]=zz;\n        ht[i][j]=k;\n        return zz;\n        \n    }\n    \n    vector<vector<int>> pacificAtlantic(vector<vector<int>>& ht) {\n        m = ht.size();\n        n = ht[0].size();\n        vector<vector<bool>> pac(m, vector<bool> (n,false));\n        vector<vector<bool>> atl(m, vector<bool> (n,false));\n        for (int i=0; i<m; i++){\n            pac[i][0]=true;\n            atl[i][n-1]=true;\n        }\n        for (int i=0; i<n; i++){\n            pac[0][i]=true;\n            atl[m-1][i]=true;\n        }\n        vector<vector<int>> res;\n        for (int i=0; i<m; i++){\n            for (int j=0; j<n; j++){\n                if (s(pac,i,j,ht) && s(atl,i,j,ht)) res.push_back({i,j});\n            }\n        }return res;\n    }\n};\n```\n\n![image](https://assets.leetcode.com/users/images/8cd20a0a-9ab1-456e-a5f3-69f05e67e3ed_1661908619.3639503.jpeg)\n
['Recursion', 'C', 'C++']