|
| 1 | +// class Solution { |
| 2 | +// public: |
| 3 | +// vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) { |
| 4 | +// int m = heights.size(), n = heights[0].size(); |
| 5 | +// int dx[] = {-1, 0, 1, 0}; |
| 6 | +// int dy[] = {0, 1, 0, -1}; |
| 7 | + |
| 8 | +// vector<vector<int>> ans; |
| 9 | + |
| 10 | +// for(int i = 0; i < m; i++) { |
| 11 | +// for(int j = 0; j < n; j++) { |
| 12 | +// bool pacific = false, atlantic = false; |
| 13 | +// queue<pair<int, pair<int, int>>> que; |
| 14 | +// vector<vector<bool>> check(m, vector<bool> (n, false)); |
| 15 | + |
| 16 | +// que.push({heights[i][j], {i, j}}); |
| 17 | +// check[i][j] = true; |
| 18 | +// while(!que.empty()) { |
| 19 | +// int h = que.front().first; |
| 20 | +// int x = que.front().second.first; |
| 21 | +// int y = que.front().second.second; |
| 22 | +// que.pop(); |
| 23 | + |
| 24 | +// for(int k = 0; k < 4; k++) { |
| 25 | +// int new_x = x + dx[k]; |
| 26 | +// int new_y = y + dy[k]; |
| 27 | + |
| 28 | +// if(new_x == -1 || new_y == -1) |
| 29 | +// pacific = true; |
| 30 | +// if(new_x == m || new_y == n) |
| 31 | +// atlantic = true; |
| 32 | + |
| 33 | +// if(-1 < new_x && new_x < m && -1 < new_y && new_y < n && check[new_x][new_y] == false && heights[new_x][new_y] <= h) { |
| 34 | +// que.push({heights[new_x][new_y], {new_x, new_y}}); |
| 35 | +// check[new_x][new_y] = true; |
| 36 | +// } |
| 37 | +// } |
| 38 | + |
| 39 | +// if(pacific && atlantic) |
| 40 | +// break; |
| 41 | +// } |
| 42 | +// if(pacific && atlantic) |
| 43 | +// ans.push_back(vector<int> {i, j}); |
| 44 | +// } |
| 45 | +// } |
| 46 | +// return ans; |
| 47 | +// } |
| 48 | +// }; |
| 49 | + |
| 50 | +class Solution { |
| 51 | +public: |
| 52 | + vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) { |
| 53 | + int dx[] = {-1, 0, 1, 0}; |
| 54 | + int dy[] = {0, 1, 0, -1}; |
| 55 | + int m = heights.size(), n = heights[0].size(); |
| 56 | + vector<vector<bool>> atlantic(m, vector<bool> (n, false)), pacific(m, vector<bool> (n, false)); |
| 57 | + queue<pair<int, int>> que; |
| 58 | + |
| 59 | + for(int i = 0; i < m; i++) { |
| 60 | + que.push({i, 0}); |
| 61 | + pacific[i][0] = true; |
| 62 | + } |
| 63 | + |
| 64 | + for(int i = 1; i < n; i++) { |
| 65 | + que.push({0, i}); |
| 66 | + pacific[0][i] = true; |
| 67 | + } |
| 68 | + |
| 69 | + while(!que.empty()) { |
| 70 | + int x = que.front().first; |
| 71 | + int y = que.front().second; |
| 72 | + que.pop(); |
| 73 | + |
| 74 | + for(int i = 0; i < 4; i++) { |
| 75 | + int nx = x + dx[i]; |
| 76 | + int ny = y + dy[i]; |
| 77 | + if(nx < 0 || nx == m || ny < 0 || ny == n) |
| 78 | + continue; |
| 79 | + |
| 80 | + if(heights[x][y] <= heights[nx][ny] && !pacific[nx][ny]) { |
| 81 | + pacific[nx][ny] = true; |
| 82 | + que.push({nx, ny}); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + for(int i = 0; i < m; i++) { |
| 88 | + que.push({i, n - 1}); |
| 89 | + atlantic[i][n - 1] = true; |
| 90 | + } |
| 91 | + |
| 92 | + for(int i = 0; i < n - 1; i++) { |
| 93 | + que.push({m - 1, i}); |
| 94 | + atlantic[m - 1][i] = true; |
| 95 | + } |
| 96 | + |
| 97 | + while(!que.empty()) { |
| 98 | + int x = que.front().first; |
| 99 | + int y = que.front().second; |
| 100 | + que.pop(); |
| 101 | + |
| 102 | + for(int i = 0; i < 4; i++) { |
| 103 | + int nx = x + dx[i]; |
| 104 | + int ny = y + dy[i]; |
| 105 | + if(nx < 0 || nx == m || ny < 0 || ny == n) |
| 106 | + continue; |
| 107 | + |
| 108 | + if(heights[x][y] <= heights[nx][ny] && !atlantic[nx][ny]) { |
| 109 | + atlantic[nx][ny] = true; |
| 110 | + que.push({nx, ny}); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + vector<vector<int>> ans; |
| 116 | + for(int i = 0; i < m; i++) |
| 117 | + for(int j = 0; j < n; j++) |
| 118 | + if(pacific[i][j] && atlantic[i][j]) |
| 119 | + ans.push_back({i, j}); |
| 120 | + return ans; |
| 121 | + } |
| 122 | +}; |
| 123 | + |
0 commit comments