Really Appreciating Your Explanation Skill. Thank you so much from bottom of my heart. More Success to You
@deepcodes Жыл бұрын
Thanks a lot Shubham!
@deepcodes Жыл бұрын
//BFS class Solution { public: const pair direction[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; int maxDistance(vector& grid) { int n = grid.size(); int visited[n][n]; queue q; for (int i = 0; i < grid.size(); i++) { for (int j = 0; j < grid[0].size(); j++) { visited[i][j] = grid[i][j]; if (grid[i][j]) { q.push({i, j}); } } } int distance = -1; while (!q.empty()) { int qSize = q.size(); while (qSize--) { pair landCell = q.front(); q.pop(); for (pair dir : direction) { int x = landCell.first + dir.first; int y = landCell.second + dir.second; if (x >= 0 && y >= 0 && x < grid.size() && y < grid[0].size() && visited[x][y] == 0) { visited[x][y] = 1; q.push({x, y}); } } } distance++; } return distance == 0 ? -1 : distance; } };
@himashisbiswas1339 Жыл бұрын
this is the best channel on youtube currently for leetcode editorials. please don't stop doing it. it helps a lot. you are going to get a lot more subscribers , i can bet on that
@deepcodes Жыл бұрын
Thanks a lot, Himashis!
@deepcodes Жыл бұрын
//DP Optimised class Solution { public: int maxDistance(vector& grid) { int n = grid.size(); int maxd = n+n+1; // vectordist(n, vector(n, maxd)); //SC: O(N^2) -> O(1) for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 1) { grid[i][j] = 0; } else { grid[i][j] = maxd; grid[i][j] = min(grid[i][j], min((i > 0 ? grid[i-1][j]+1 : maxd), (j>0 ? grid[i][j-1]+1: maxd))); } } } int ans = INT_MIN; for (int i = n-1; i >= 0; i--) { for (int j = n-1; j >= 0; j--) { grid[i][j] = min(grid[i][j], min((i
@deepcodes Жыл бұрын
//DP class Solution { public: int maxDistance(vector& grid) { int n = grid.size(); int maxd = n+n+1; vectordist(n, vector(n, maxd)); //SC: O(N^2) -> O(1) for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 1) { dist[i][j] = 0; } else { dist[i][j] = min(dist[i][j], min((i > 0 ? dist[i-1][j]+1 : maxd), (j>0 ? dist[i][j-1]+1: maxd))); } } } int ans = INT_MIN; for (int i = n-1; i >= 0; i--) { for (int j = n-1; j >= 0; j--) { dist[i][j] = min(dist[i][j], min((i