diff --git a/Pacific_Atlantic_Water_Flow.cpp b/Pacific_Atlantic_Water_Flow.cpp new file mode 100644 index 0000000..83200d2 --- /dev/null +++ b/Pacific_Atlantic_Water_Flow.cpp @@ -0,0 +1,46 @@ +class Solution { +private: + void bfs(vector>& heights, queue>& q, vector>& vis){ + vector dir = {0, 1, 0, -1, 0}; + while(!q.empty()){ + auto x = q.front(); + q.pop(); + for(int i=0;i<4;i++){ + int xi = x.first+dir[i]; + int yi = x.second+dir[i+1]; + if(xi>=0 && yi>=0 && xi=heights[x.first][x.second]){ + vis[xi][yi] = true; + q.push({xi, yi}); + } + } + } + } +public: + vector> pacificAtlantic(vector>& heights) { + queue> pacQ; + queue> atlQ; + vector> pacVis(heights.size(), vector(heights[0].size(), false)); + vector> atlVis(heights.size(), vector(heights[0].size(), false)); + for(int i=0;i> ans; + for(int i=0;i