id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\n vector<int> rowInd;\n vector<int> colInd;\n void processCell(int row, int col, vector<vector<int>> &mat) const{\n // cout << row << \", \" << col << endl;\n int minNeighbor = INT_MAX;\n int n = mat.size();\n int m = mat[0].size();\n for(unsigne...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n // BFS\n // starting with all the 0s\n // before enqueue, update matrix with steps\n // and mark as visited\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<int> queue;\n const int rows = mat.size();\n const int cols = ma...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n void dfs(int i,int j,int step,vector<vector<int>> &vis, vector<vector<int>>& mat){\n vis[i][j]=step;\n if(i-1>=0 && vis[i-1][j]>1+step && mat[i-1][j]==1) dfs(i-1,j,step+1,vis,mat);\n if(j-1>=0 && vis[i][j-1]>1+step && mat[i][j-1]==1) dfs(i,j-1,step+1,vis,...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n void distance(const vector<vector<int>>& mat, vector<vector<int>>& output, int x, int y, int acc) {\n if (x < 0 || x >= mat.size() || y < 0 || y >= mat[x].size()) {\n return;\n }\n if (mat[x][y] == 0) {\n acc = 0;\n } \n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n\n int row[4]={1,0,-1,0};\n int col[4]={0,-1,0,1};\n int n,m;\n void solve(int i,int j,vector<vector<int>>&dis,vector<vector<int>>&mat){\n\n for(int k=0;k<4;k++){\n int nr=i+row[k];\n int nc=j+col[k];\n if(nr<n and nc<m and nc>=...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> costMatrix(mat.size(), vector<int>(mat[0].size(), INT_MAX));\n\n for(int i = 0; i < mat.size(); i++) {\n for(int j = 0; j < mat[0].size(); j++) {\n if(ma...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> distance;\n int value(int row, int col) {\n if (row < 0 || col < 0 || row >= distance.size() ||\n col >= distance[0].size()) {\n return INT_MAX;\n }\n return distance[row][col];\n }\n void solve(vector<ve...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\n static constexpr std::pair<int,int> dirs[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int i, j;\n int rows, cols;\n std::queue<std::pair<int, int>> bfsQueue;\n\n rows = mat.size();\n...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n if (mat.empty() || mat[0].empty())\n return {};\n\n int m = mat.size(), n = mat[0].size();\n queue<pair<int, int>> queue;\n int MAX_VALUE = m * n;\n \n for (int...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
0
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int row = mat.size(), col = mat[0].size();\n queue<pair<int, int>> q;\n vector<vector<int>> vis(row, vector<int>(col, -1));\n for (int i = 0; i < row; i++) {\n for (int j = 0...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\nvector<vector<int>>dir={{1,0},{0,1},{-1,0},{0,-1}};\nvoid bfs(vector<vector<int>>& mat,vector<vector<int>>&ans,queue<pair<int,int>>&q){\n int level=0;\n while(q.size()>0){\n auto curr=q.front();\n int r=curr.first;\n int c=curr.second;\n q.pop();\...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m=mat.size();\n int n=mat[0].size();\n queue<pair<int,int>> que;\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<n;j++)\n {\n if(mat[i][j]==0)...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& grid) {\n int n = grid.size(),m = grid[0].size();\n vector<vector<bool>> vis(n,vector<bool>(m,false));\n queue<int> que;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(), m = mat[0].size();\n std::queue<std::pair<int,int>> q;\n std::vector<std::vector<bool>> vis(n, std::vector<bool>(m, false));\n int cnt = 1;\n for (int i = 0; ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int rows = mat.size();\n int cols = mat[0].size();\n queue<pair<int,int>> q;\n vector<pair<int,int>> directions = {{-1,0},{1,0},{0,-1},{0,1}} ;\n for(int i = 0; i < rows; i++){\n...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n bool isValid(int i, int j, vector<vector<int>>&mat){\n int m=mat.size();\n int n=mat[0].size();\n if(i<0||i>=m||j<0||j>=n)return 0;\n if(mat[i][j]==1)return 1;\n return 0;\n }\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m=mat.size();\n int n=mat[0].size();\n vector<vector<int>>ans(m,vector<int>(n,0));\n queue<pair<pair<int,int>,int>>q;\n int delrow[]={-1,0,1,0};\n int delcol[]={0,-1,0...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<int,pair<int,int>>> q;\n //dist, cell\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<bool>> visited(m, vector<bool>(n,false));\n for(int r=0;r<m...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<pair<int,int>,int>>q;\n int n=mat.size();\n int m=mat[0].size();\n vector<vector<bool>>vis(n,vector<bool>(m,false));\n \n for(int i=0;i<n;i++){\n for...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\nvector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<int>> res(m, vector<int>(n, 0));\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n int r[] = {1, -1, 0, 0};\n int c[] = {0, 0, ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n bool check(vector<vector<int>>& mat, int i, int j, int m, int n, vector<vector<bool>>&visited){\n if (i >= 0 && i < m && j >= 0 && j < n){\n if (mat[i][j] == 1 && visited[i][j] == false){\n return true;\n }\n }\n retur...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) \n {\n //bfs\n vector<vector<int>> visited=mat;\n queue<pair<pair<int, int>, int>> q;\n for(int i=0; i<mat.size(); i++)\n {\n for(int j=0; j<mat[0].size(); j++)\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<int,pair<int,int>>>q;\n \n int n = mat.size();\n int m = mat[0].size();\n for(int i = 0 ; i< mat.size(); i++){\n for(int j = 0 ; j<mat[0].size();j++...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
2
{ "code": "class Solution {\nprivate:\n void bfs(vector<vector<int>>& mat, vector<vector<int>>& visited, vector<vector<int>>& dist){\n\n int n = mat.size();\n int m = mat[0].size();\n\n queue<pair<pair<int,int>,int>> q;\n\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n vector<vector<int>> vis(n, vector<int>(m, 0)); \n\t vector<vector<int>> ans(n, vector<int>(m, 0)); \n queue<pair<pair<int,int>,int>> q;...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(), m = mat[0].size();\n\n vector<vector<int>> ans(n, vector<int>(m, -1));\n\n queue<pair<int,pair<int,int>>> q;\n int visit[n][m];\n memset(visit, 0, sizeof(visi...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<int,pair<int,int>>> q;\n int m = mat.size();\n int n = mat[0].size();\n for(int i=0;i<m;i++){\n for(int j=0;j<n;j++){\n if(mat[i][j]==0) q.push({0, ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\n size_t rows, columns;\n vector<vector<int>> distances;\n\n void foreachAdj(size_t r, size_t c, auto f) {\n if (r > 0)\n f(r - 1, c);\n if (c > 0)\n f(r, c - 1);\n if (r < rows - 1)\n f(r + 1, c);\n if (c < columns - 1)...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n vector<vector<int>>ans(n, vector<int>(m,0));\n queue<pair<int,int>>q;\n for (int i = 0;i<n;i++) {\n for (int j = 0;j<m;j...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) \n {\n deque<tuple<int, int, int>> q;\n for (int i = 0; i < mat.size(); ++i)\n {\n for (int j = 0; j < mat[i].size(); ++j)\n {\n if (mat[i][j] == 0)\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n int rows[4] = {1,-1,0,0};\n int cols[4] = {0,0,1,-1};\n\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<int>> ans(m, vector<int>(n,0));\n vector<vector<int>> visite...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int rows = mat.size();\n int cols = mat[0].size();\n vector<vector<bool>>visited(rows, vector<bool>(cols));\n queue<pair<int, int>> q;\n vector<vector<int>> ans(rows, vector<int>...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\nvoid bfs(vector<vector<int>> mat,queue<pair<int,pair<int,int>>> zeros,vector<vector<int>>& matnew){\n vector<pair<int,int>> trav={{1,0},{-1,0},{0,1},{0,-1}};\n while(!zeros.empty()){\n int r=zeros.front().second.first;\n int l=zeros.front().first+1;\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> bfs(queue<pair<pair<int,int>, int>> q, vector<vector<int>>& adj, vector<vector<int>>& vis){\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n int n = adj.size();\n int m = adj[0].size();\n vector<vector...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> bfs(queue<pair<pair<int,int>, int>> q, vector<vector<int>>& adj, vector<vector<int>>& vis){\n vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};\n int n = adj.size();\n int m = adj[0].size();\n vector<vector...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>>visited;\n int rows,cols;\n vector<vector<int>>newM;\n vector<pair<int,int>>dir={{-1,0},{1,0},{0,-1},{0,1}};\n \n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n rows=mat.size();\n cols=mat[0].size();\n new...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<int>> mat, vector<vector<int>> &vis,vector<vector<int>>&ans,queue<pair<pair<int,int>,int>>&q){\n int delrow[]={1,0,-1,0};\n int delcol[]={0,1,0,-1};\n while(!q.empty()){\n int size= q.size();\n for(int i=0;i<si...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> tr(mat.size(), vector<int>(mat[0].size(), -1));\n map<pair<int, int>, bool> pts;\n for (int i = 0; i < mat.size(); i++)\n for (int j = 0; j < mat[0].size(); j++)...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\n private:\n deque<pair<int,int>> edgeCases;\n vector<vector<int>> solution;\n int count = 0;\n bool isValid(pair<int,int> pos){\n if(pos.first < 0 || pos.second < 0 || pos.first >= solution.size() || pos.second >= solution[0].size()) return false;...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n\n int dx[4] = {0, 0, 1, -1};\n int dy[4] = {1, -1, 0, 0};\n\n int n = mat.size(), m = mat[0].size();\n\n vector<vector<int>> dis(n, vector<int>(m,INT_MAX));\n\n vector<vector<boo...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n bool valid(int x, int y, int s, int ss) {\n if (x < 0 || y < 0)\n return false;\n\n if (x >= s || y >= ss)\n return false;\n\n return true;\n }\n\n void BFS(queue<pair<pair<int, int>, int>> q, vector<vector<int>>& grid,\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n \n int m = mat.size();\n int n = mat[0].size();\n\n int max_dist = max(m, n);\n\n vector<vector<int>> mat_dist(m, vector<int>(n, max_dist));\n\n queue<pair<int,int>> q;\n\...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "#define FOR(i, start, end) for (int i = start; i < end; ++i)\n\nclass Cell {\npublic:\n int row, col;\n Cell() {}\n Cell(int r, int c) : row(r), col(c) {}\n};\nclass Solution {\n vector<vector<int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};\n\npublic:\n vector<vector<int>> updateMatri...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n typedef pair<int, int> pi;\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n vector<vector<int>> ans(n, vector<int>(m, 1e8));\n vector<vector<int>> vis(n, vector<int> (m, 0));\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "// class Solution {\n// public:\n// void bfs(int i, int j, vector<vector<int>>&mat, vector<vector<int>>&dist){\n// int n = mat.size();\n// int m = mat[0].size();\n// queue<pair<int,int>>q1;\n// q1.push({i,j});\n// while(q1.size() != 0){\n// pair<int,i...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& grid) {\n int n = grid.size(); int m = grid[0].size();\n\n\n vector<vector<int>> dist(n ,vector<int> (m, INT_MAX));\n vector<vector<int>> vis(n, vector<int> (m, 0));\n\n queue<pair<pair<int,int>...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<int>>up(m,vector<int>(n,m*n));\n vector<vector<int>>down(m,vector<int>(n,m*n));\n vector<vector<int>>left(m,vector<in...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n void BFS(queue<pair<pair<int,int>,int>> q,int n,int m,vector<vector<int>> &vis,vector<vector<int>> mat,vector<vector<int>> &ans){\n vector<int> delRow ={-1,0,1,0}; \n vector<int> delCol ={0,-1,0,1}; \n \n while(!q.empty()){\n int i = q.f...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n queue<pair<pair<int,int>,int>> points;\n vector<vector<int>> matrix;\n int rows=0;\n int cols=0;\n\n void processPoint(pair<pair<int,int>,int> point){\n int row = point.first.first;\n int col = point.first.second;\n int distance = point.second...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> ans(mat.size());\n vector<pair<int,int>>vec;\n for(int i = 0 ;i < mat.size();++i){\n for(int j = 0; j < mat[i].size();++j){\n ans[i].push_back(-...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> ans(mat.size());\n vector<pair<int,int>>vec;\n for(int i = 0 ;i < mat.size();++i){\n for(int j = 0; j < mat[i].size();++j){\n ans[i].push_back(-...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> bfs ( vector<vector<int>>& grid, vector <pair<int, int>> sources, vector <vector<bool>> &visited){\n\n int nrows = grid.size(), ncolumns = grid[0].size();\n queue <pair<int,int>> parent, child, empty;\n\n for ( auto source : sources ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n class Node {\n public:\n int row, col, dist;\n Node(int row, int col, int dist) {\n this->row = row;\n this->col = col;\n this->dist = dist;\n }\n };\n\n int dx[4] = {-1, 1, 0, 0};\n int dy[4] = {0, 0, 1, -1};\...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\n vector<pair<int,int>>dirs = { {1,0},{0,1},{-1,0},{0,-1} };\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n \n list<pair<int,pair<int,int>>>myList; // time and coords \n int rows = mat.size();\n int cols = mat[0].size();\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue <tuple<int,int,int>> q;\n vector<vector<int>> v(mat.size(),vector<int>(mat[0].size(),INT_MAX));\n for(int i = 0; i < mat.size(); i++){\n for(int j = 0; j < mat[i].size(); j++)...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int rows = mat.size(), cols = mat[0].size();\n vector<vector<int>> dist(rows, vector<int>(cols, INT_MAX));\n int vis = 0;\n queue<pair<int,int>> q;\n int level = 0;\n for(...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(), m = mat[0].size();\n vector<vector<int>> ans(n, vector<int>(m, 1e9));\n for(int i = 0; i < n;i++){\n for(int j = 0; j < m;j++){\n if(mat[i][j] == ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\nqueue<pair<int,int>>q; vector<vector<int>>dp;set<pair<int,int>>st;\nvector<vector<int>>dir={{0,1},{0,-1},{1,0},{-1,0}};\nint f(queue<pair<int,int>>&q,vector<vector<int>>&v){\nint t=0;\nwhile(!q.empty()){\nint s=q.size();\nwhile(s--){\nauto el=q.front();\nint r=el.first;\nint c...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int rows = mat.size(), cols = mat[0].size();\n vector<vector<int>> dist(rows, vector<int>(cols));\n int vis = 0;\n queue<pair<int,int>> q;\n int level = 0;\n for(int i=0;i...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> dst(mat.size(), vector<int>(mat[0].size(), 0));\n\n std::queue<vector<int>> steps;\n for (int i = 0; i < mat.size(); ++i) {\n for (int j = 0; j < mat[0].size(); ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n void bfs(queue<pair<pair<int , int> , int>>&q, vector<vector<int>>&grid , vector<vector<int>>&distance, int a, int b){\n while(!q.empty()){\n int i = q.front().first.first;\n int j = q.front().first.second;\n int k = q.front().second;\n...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n=mat.size(),m=mat[0].size();\n vector<vector<int>> v(n, vector<int> (m,INT_MAX));\n queue<pair<pair<int,int>,int>> q;\n \n for(int i=0;i<n;i++){\n for(int j=0;j<m...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int rows = mat.size();\n int cols = mat[0].size();\n \n queue<pair<pair<int,int>,int>> q; // Queue to store {i,j,distance}\n \n // Initialize the queue with all zero cells...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n queue<pair<int,int>> q;\n vector<vector<int>> vis(n, vector<int> (m,0));\n for(int i= 0;i<n;i++){\n for(int j=0;j<m;j++)...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size();\n int m = mat[0].size();\n\n vector<vector<int>> dist (n,vector<int>(m,-1));\n\n queue<pair<int,int>> q;\n\n for(int i=0;i<n;++i){\n for(int j=0;j<...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>>output(mat.size(),vector<int>(mat[0].size(),-1));\n queue<pair<int,int>>q;\n\n for(int i=0;i<mat.size();i++){\n for(int j=0;j<mat[0].size();j++){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(const vector<vector<int>>& mat) {\n const int R = mat.size();\n const int C = mat[0].size();\n vector<vector<int>> result;\n for (int i = 0; i < R; ++i) {\n result.push_back(vector<int>(C, -1));\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n unordered_set<int> v;\n queue<pair<int,int>>q;\n int r = mat.size();\n int c = mat[0].size();\n for(int i=0;i<r;i++){\n for(int j =0;j<c;j++){\n if(mat[...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int count = 0, step = 0;\n vector<vector<int>> ans = mat;\n vector<vector<bool>> vis = vector(mat.size(), vector(mat[0].size(), false));\n queue<pair<int, int>> q;\n for (int i =...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "#define code ios_base::sync_with_stdio(false);\n#define by cin.tie(NULL);\n#define bitandas cout.tie(NULL);\n\nclass Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n code by bitandas;\n int m = mat.size(), n = mat[0].size();;\n vector<vector<int>>...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n using state=pair<int,int>;\n vector<vector<int>>vis;\n vector<vector<int>>dis;\n int N,M;\n bool isvalid(int x,int y){\n if(0<=x && x<N && 0<=y && y<M && vis[x][y]==0)\n return 1;\n return 0;\n }\n vector<state> neigh(state next){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n //using BFS\n //it is just like rotten tomatoes\n //initialise queue with already 0's\n vector<vector<int>> ans(mat.size(),vector<int>(mat[0].size(),-1));\n queue<pair<int,pair<i...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat)\n {\n int n= mat.size(), m = mat[0]. size();\n vector< vector<int> >ans(n,vector<int> (m,INT_MAX));\n\n queue<vector<int>>q;\n\n for(int i=0; i<n; i++)\n { \n for(int j...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat)\n {\n int n= mat.size(), m = mat[0]. size();\n vector< vector<int> >ans(n,vector<int> (m,INT_MAX));\n\n queue<vector<int>>q;\n\n for(int i=0; i<n; i++)\n { \n for(int j...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int height = mat.size();\n int length = mat[0].size();\n queue<pair<int,pair<int,int>>> q;\n unordered_set<int> visited;\n for (int i = 0; i < mat.size(); i++) {\n for...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n\n vector<vector<int>> res = mat;\n vector<vector<bool>> visited(m, vector<bool>(n, false));\n queue<pair<int,int>> q;\n\n fo...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size(),n=mat[0].size();\n queue<pair<int,pair<int,int>>>q;\n vector<vector<int>>vis(m,vector<int>(n,0));\n vector<vector<int>>dis(m,vector<int>(n,0));\n\n for(int i=0...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(); \n int m = mat[0].size();\n set<pair<int, int>> q;\n vector<int> foo (m, -1);\n vector<vector<int>> ret (n, foo);\n \n for(int i = 0; i < n; i++...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m=mat.size();\n int n=mat[0].size();\n \n vector<vector<int>> ans(m,vector<int>(n));\n vector<vector<bool>> visited(m,vector<bool>(n));\n \n queue<vector<int>> q;...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<pair<int,int>> dir = {{1,0}, {0,1}, {-1,0}, {0,-1}};\n\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n unordered_map<int,int> visited;\n unordered_set<int> seen;\n vec...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& v) {\n int n=v.size(),m=v[0].size();\n vector<vector<int>>visited(n,vector<int>(m,0));\n queue<vector<int>>q;\n for(int i=0;i<n;i++){\n for(int j=0;j<m;j++){\n if(!v[i]...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<pair<int,int>,int>>q;\n int rows=mat.size();\n int cols=mat[0].size();\n vector<vector<int>>visited(rows,vector<int>(cols,0));\n \n vector<vector<int>>distance(...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n vector<vector<int>> vec(m, vector<int>(n, -1));\n vector<vector<int>> visited(m, vector<int>(n, 0));\n queue<pair<pair<int, int>, i...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n if (mat.empty()) {\n return {};\n }\n const size_t height = mat.size();\n const size_t width = mat.back().size();\n deque<pair<size_t, size_t>> queue;\n vector<...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n //similar to rotten oranges\n int n=mat.size();\n int m=mat[0].size();\n int curr=0;\n vector<vector<int>>ans(n,vector<int>(m,INT_MAX));\n vector<vector<int>>visited(n,vec...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n\n int m = mat.size();\n int n = mat[0].size();\n\n vector<vector<int>> result(m, vector<int>(n,0));\n vector<vector<int>> visited(m, vector<int>(n, 0));\n vector<pair<int,int>> z...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n map<pair<int,int>, bool> visited;\n queue<pair<pair<int,int>,int>> q;\n for( int i = 0 ; i< m;i++){\n for( int j = 0 ; j...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n void bfs(vector<vector<int>>& mat,set<pair<int,int>>&st){\n queue<pair<pair<int,int>,int>>q;\n for(int i=0;i<mat.size();i++){\n for(int j=0;j<mat[0].size();j++){\n if(mat[i][j]==0){\n q.push({{i,j},0});\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n int dx[4] = {-1, 0, +1, 0};\n int dy[4] = {0, +1, 0, -1};\n void bfs(queue<pair<int,pair<int,int>>>& q, int m, int n, vector<vector<int>>& mat, set<pair<int,int> >& st, vector<vector<int>>& res) {\n while(!q.empty()) {\n auto curr = q.front(); q.pop(...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n queue<pair<int,pair<int,int>>> q;\n map<pair<int,int>,int> vis;\n int n = mat.size();\n int m = mat[0].size();\n for(int i=0;i<mat.size();i++)\n {\n for(int j=0...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n int dr[4]={1,0,-1,0};\n int dc[4]={0,1,0,-1};\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int m = mat.size();\n int n = mat[0].size();\n map<pair<int,int>,int>vis;\n vector<vector<int>>ans(m,vector<int>(n,0));\n qu...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n // could have been solved by doing bfs for each and every cell which contains 1, but TLE\n vector<vector<int>> discalc(vector<vector<int>>& mat)\n {\n int n=mat.size(); int m=mat[0].size();\n vector<vector<int>> dis(n,vector<int>(m,INT_MAX));\n\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n // could have been solved by doing bfs for each and every cell which contains 1, but TLE\n vector<vector<int>> discalc(vector<vector<int>>& mat)\n {\n int n=mat.size(); int m=mat[0].size();\n vector<vector<int>> dis(n,vector<int>(m,INT_MAX));\n\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n\n int dx[4]={-1,0,1,0};\n int dy[4]={0,1,0,-1};\n int n,m;\n\n // vector<vector<int>>visit;\n \n // int dfs(vector<vector<int>>&arr,int x,int y)\n // {\n // if(arr[x][y]==0)return 0;\n\n // visit[x][y]=1;\n\n // int ans=INT_MAX;\n\n /...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>> grid)\n{\n int n = grid.size();\n int m = grid[0].size();\n vector<vector<int>> vis(n, vector<int>(m, 0));\n vector<vector<int>> dist(n, vector<int>(m, 0));\n queue<pair<pair<int, int>, int>> q;\n for (int...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n vector<vector<int>> res;\n queue<vector<int>> discovery_nodes;\n vector<vector<int>> dirs = {{0,1}, {0,-1}, {1,0}, {-1,0}};\n\n // init res\n for(int i=0; i<mat.size(); i++){\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n bool update(vector<vector<int>>& mat, vector<vector<int>>& r, int i, int j, int m, int n, int d) {\n if (i < 0 || i >= m || j < 0 || j >= n) {\n return false;\n }\n if (mat[i][j] != 0) {\n r[i][j] = std::min(d + 1, r[i][j]);\n ...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dir = {{1,0},{0,1},{-1,0},{0,-1}} ;\n void bfs(queue<pair<int,int>>& q ,set<pair<int,int>>& visited,vector<vector<int>>& ans){\n int dist = 0 ;\n while(q.empty()==0){\n auto p = q.front() ;\n q.pop() ;\n if...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> dir = {{1,0},{0,1},{-1,0},{0,-1}} ;\n void bfs(queue<pair<int,int>>& q ,set<pair<int,int>>& visited,vector<vector<int>>& ans){\n int dist = 0 ;\n while(q.empty()==0){\n auto p = q.front() ;\n q.pop() ;\n if...
542
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p> <p>The distance between two adjacent cells is <code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetc...
3
{ "code": "class Solution {\npublic:\n vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {\n int n = mat.size(), m= mat[0].size();\n\n vector<vector<int> > dist(n, vector<int>(m, INT_MAX));\n priority_queue< pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<in...