Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell. The distance between two adjacent cells is 1. Link to challenge: leetcode.com/e...
You don't need an additional answer array, you can directly modify the original array by adding '-1' and use it, so that the space complexity is just for the additional queue you are using.
I really liked it. one arrow 2 birds, learnt Graphs min distance and this problem. i mean i really haven't understood other channels videos about this problem, honestly at first 2 mins i had same disappointment as with other channels but gradually at 6 and 7th min i got your way into mind. completely without a doubt easily understandable, you got new subscriber. Thank god i can sleep peacefully today.
Hey Alisha can you pleases tell me why dfs with memoization isn't working here.....although for the same problem recursion is working fine, See this is my code please help if I am conceptually wrong somewhere. class Solution { public: int R; int C; vector dp; vector updateMatrix(vector& mat) { R = mat.size(); C = mat[0].size(); vector ans(R, vector(C, 999999)); dp.resize(R, vector(C, 999999)); for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { vector visited(R, vector(C, -1)); dp.resize(R, vector(C, 999999)); ans[i][j] = dfs(i, j, mat, visited); } }
return ans; } int dfs(int i, int j, vector mat, vector& visited) { if (i < 0 || i >= R || j < 0 || j >= C) { return 999999; } if (visited[i][j] == 1) return 999999; if (mat[i][j] == 0) return 0; if (dp[i][j] != 999999) return dp[i][j]; visited[i][j] = 1; int top = 1 + dfs(i - 1, j, mat, visited); int bot = 1 + dfs(i + 1, j, mat, visited); int left = 1 + dfs(i, j - 1, mat, visited); int right = 1 + dfs(i, j + 1, mat, visited); visited[i][j] = -1; return dp[i][j] = min(top, min(bot, min(left, right))); } }; For this input its wrong [[1,0,1,1,0,0,1,0,0,1],[0,1,1,0,1,0,1,0,1,1],[0,0,1,0,1,0,0,1,0,0],[1,0,1,0,1,1,1,1,1,1],[0,1,0,1,1,0,0,0,0,1],[0,0,1,0,1,1,1,0,1,0],[0,1,0,1,0,1,0,0,1,1],[1,0,0,0,1,1,1,1,0,1],[1,1,1,1,1,1,1,0,1,0],[1,1,1,1,0,1,0,0,1,1]]
Thankyou so much for this solution its very easy to understand and grasp, U are from IIT with metallurgical engineering. A big respect for a big level of hardwork to reach at this position. Thanks again.
Everytime I see you video in recommendation page, I just don't even think of other explanation videos ❤ Love from NIT Silchar ✌
Also send some water from NIT Silchar, so we can go to campus again 😂
Very great explanation. The way you explains, it will make easy to understand the logic. ❤ Love from IIIT Dharwad
The IIT Bombay girl explained it the best! Thank you. You're a huge inspiration :)
You don't need an additional answer array, you can directly modify the original array by adding '-1' and use it, so that the space complexity is just for the additional queue you are using.
I really liked it. one arrow 2 birds, learnt Graphs min distance and this problem.
i mean i really haven't understood other channels videos about this problem, honestly at first 2 mins i had same disappointment as with other channels but gradually at 6 and 7th min i got your way into mind.
completely without a doubt easily understandable, you got new subscriber.
Thank god i can sleep peacefully today.
Thank you so much! Glad it was helpful
gurl, not dying to watch ur vdeos
Thanks a lot, I was able to understand and solve the leetcode problem because of your great explanation!
Hey Alisha can you pleases tell me why dfs with memoization isn't working here.....although for the same problem recursion is working fine,
See this is my code please help if I am conceptually wrong somewhere.
class Solution {
public:
int R;
int C;
vector dp;
vector updateMatrix(vector& mat) {
R = mat.size();
C = mat[0].size();
vector ans(R, vector(C, 999999));
dp.resize(R, vector(C, 999999));
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
vector visited(R, vector(C, -1));
dp.resize(R, vector(C, 999999));
ans[i][j] = dfs(i, j, mat, visited);
}
}
return ans;
}
int dfs(int i, int j, vector mat, vector& visited) {
if (i < 0 || i >= R || j < 0 || j >= C) {
return 999999;
}
if (visited[i][j] == 1)
return 999999;
if (mat[i][j] == 0)
return 0;
if (dp[i][j] != 999999)
return dp[i][j];
visited[i][j] = 1;
int top = 1 + dfs(i - 1, j, mat, visited);
int bot = 1 + dfs(i + 1, j, mat, visited);
int left = 1 + dfs(i, j - 1, mat, visited);
int right = 1 + dfs(i, j + 1, mat, visited);
visited[i][j] = -1;
return dp[i][j] = min(top, min(bot, min(left, right)));
}
};
For this input its wrong
[[1,0,1,1,0,0,1,0,0,1],[0,1,1,0,1,0,1,0,1,1],[0,0,1,0,1,0,0,1,0,0],[1,0,1,0,1,1,1,1,1,1],[0,1,0,1,1,0,0,0,0,1],[0,0,1,0,1,1,1,0,1,0],[0,1,0,1,0,1,0,0,1,1],[1,0,0,0,1,1,1,1,0,1],[1,1,1,1,1,1,1,0,1,0],[1,1,1,1,0,1,0,0,1,1]]
Same doubt
Same doubt (2)
Same doubt, I initially solved using that.
The code doesn't work, thoughthe logic seems fine. It shows TLE.
at 9:21 you forgot to push the coordinate (1,1) into the queue. but the explanation of concept is good.
Also in queue example you did not add (1,1)
good explanation ..
Hi can you post the solution link aswell it will be helpful.
no words ur explanation simply superb✌✌✌
Great video ❤ thanks a lot 🙏
Nice explanation Di ❣️
I am having à doubt since for shortest distance We Can use Floyd warshall also so Why Here Bfs is preffered only ?
But Now the question has changed ig in the leetcode platform
thanks alisha
Thanks bro..❤
Amazing explanation
what's the time complexity of the code?
Great explanation
history still can't explain how can one open so many youtube tabs in browser😁
why do we need to check visited or not ?
Thankyou so much for this solution its very easy to understand and grasp, U are from IIT with metallurgical engineering. A big respect for a big level of hardwork to reach at this position. Thanks again.
Great explanation as always. Thank you !!!
Very nice explanation thanks 🙏
very helpful thank you very much
thank you mam
Wonderful explanation!
I finally understand BFS lol
class Solution {
public:
bool checkValid(int i,int j,int m,int n){
if(i=n){
return false;
}
return true;
}
vector updateMatrix(vector& mat) {
queueq;
int m=mat.size();
int n=mat[0].size();
for(int i=0;i
Good explanation carry on
good one.
great explanation🙌
Didi. Can you please explain why can't we use a DFS approach here?
You can but it will complicate the logic but BFS any way gives you nearest distance always so BFS is preferred.
Woah🔥
very nice explanation ,thx
Thank you
use openboard
Amazing explanation.
great explanation!
Very good
Clear explanation👏
code link ??
U don't even use language which I use. I feel, u don't know coding
Kaam kr apna, mere against krne k liye log ko calls na kr
lol u tlk funny
mind ur business
awesome explanation
Amazing explanation