Similar Problems and very good for practice. Leetcode 286. Walls and Gates Leetcode 417. Pacific Atlantic Water Flow Leetcode 994. Rotting Oranges Leetcode 1162. As Far from Land as Possible Leetcode 1765. Map of Highest Peak
Very thankful for giving the similar problem list leetcode graph medium ko aapne array asa simple kar dyia bas logic socho implement to koi bhi kar le.
I stopped watching the video at 7 minutes then I went and code on my own. I used a visited array to encounter the visited elements I got TLE. Then I watched the full video and used set. I never though that we could use set also to keep the visited array. Such a great explanation! ❣
Its possible to solve with visited array.Maybe you got TLE because of some infinite recursion. I solved with the following code : class Solution { private: int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
bool within_bound(int i, int j, int n){ return (i>=0 && i=0 && j
Bhai kaha se hain aap main really kabhi na kabhi apse milna chahta hu..😛mjhe graph padhna tha par apke jaisa koi nhi padhta phr mjhe apka channel milaa to leetcode challenge shuru Kiya aur graphs bhii..love you bhaii may Allah bless you keep shining 🙏🙏
codestorywithMIK is my goto channel for daily streak questions. I wait for you to upload the video instead of watching someone else's video as here we learn to build the intuition. Thank you Sir ❤
As always again you are amazing sir🔥, please please make video on leetcode contest upsolving i am eagerly waiting for this series because after giving contest i can't find any solid solutions for those problems which i was unable to solve , it will really helpful for every leetcoders whom giving contest seriously , it's a huge request sir🙏🙏
Thanks a lot Ashutosh. I understand and will definitely plan to cover contests soon. Currently a little occupied because of DP concepts playlist But for sure soon
Thank you KING of Graphs 👑👑 Java implementation: PS : Code might look lengthy because I have overridden equals and hashCode method to use Pair class in set. But this would work for any future code where you need this conversion as well. class Pair { int row; int col;
Pair(int _row, int _col) { row = _row; col = _col; }
@Override public int hashCode() { int hash = 3; hash = 47 * hash + this.row; hash = 47 * hash + this.col; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Pair other = (Pair) obj; if (this.row != other.row) { return false; } if (this.col != other.col) { return false; } return true; } } class Solution {
First I did a brute force approach. I stored all cells of first island by dfs. And then store the cells of second island and find the Manhattan distance for every combination of both islands. This got TLE. Then did multi source BFS until i got a 1 from second island.. got accepted
Good explanation 👌 Could you please make a video of today's daily challenge like how to solve it with the help of bucket sort because solving it with the help of priority queue is simple and understandable but there are very less videos explaning how to use bucket sort for this question.
Hi mik , i completed graph concept playlist . just a request can you make vdeo on how to find cut vertex and cut edge ie bridges in graph . It will helpful for sem exam too .
Here's the Java code Tip: Java has a inbuilt Pair already, no need to make a class like others keep doing class Solution { int n, m; int[][] directions = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}}; public boolean inBorder(int[][] grid, int i, int j) { return i >= 0 && j >= 0 && j < m && i < n; } public void dfs(int[][] grid, int i, int j, Set visited) { if (!inBorder(grid, i, j) || visited.contains(new Pair(i, j)) || grid[i][j] == 0) return; visited.add(new Pair(i, j)); for (int[] dir : directions) { int _x = i + dir[0]; int _y = j + dir[1]; dfs(grid, _x, _y, visited); } } public int bfs(int[][] grid, int i, int j, Set visited) { Queue queue = new LinkedList(); for (Pair p : visited) { queue.add(p); } int level = 0; while (!queue.isEmpty()) { int size = queue.size(); while (size-- > 0) { Pair p = queue.poll(); int first = p.getKey(); int second = p.getValue(); for (int[] dir : directions) { int _x = first + dir[0]; int _y = second + dir[1]; if (inBorder(grid, _x, _y) && !visited.contains(new Pair(_x, _y))) { if (grid[_x][_y] == 1) { return level; } visited.add(new Pair(_x, _y)); queue.add(new Pair(_x, _y)); } } } level++; } return level; } public int shortestBridge(int[][] grid) { n = grid.length; m = grid[0].length; Set visited = new HashSet(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (grid[i][j] == 1) { dfs(grid, i, j, visited); return bfs(grid, i, j, visited); } } } return -1; } }
I think , I had already made a video on Bipartite Graph long back. So i missed to solve it for streak this time. But dont worry, i will cover it with my coin 😁
Tussi great ho sirji 🙌🏻 ✨ Every day I wait for your video 😍
Means a lot ❤️❤️❤️
Similar Problems and very good for practice.
Leetcode 286. Walls and Gates
Leetcode 417. Pacific Atlantic Water Flow
Leetcode 994. Rotting Oranges
Leetcode 1162. As Far from Land as Possible
Leetcode 1765. Map of Highest Peak
kkya matlab ye sb questions Graph Conepts waale playlist mein aane wala h 🤣😀
Why not 🤓
Very thankful for giving the similar problem list leetcode graph medium ko aapne array asa simple kar dyia bas logic socho implement to koi bhi kar le.
Thanks a lot Ankit ❤️❤️
thank u so much bhaiya
As always , L.E.G.E.N.D.
From those story points, I stopped the video and started coding 🥳.
No one can beat your teaching style.
Wow bhai, you are a rockstar too
Similar problem mentioning in Comment section is very nice.
Please continue this. Love and Peace from my side.
Thanks a lot Dhruv ❤️❤️
I stopped watching the video at 7 minutes then I went and code on my own. I used a visited array to encounter the visited elements I got TLE. Then I watched the full video and used set. I never though that we could use set also to keep the visited array. Such a great explanation! ❣
Wow. Thanks a lot
I am so glad ❤️❤️❤️
Its possible to solve with visited array.Maybe you got TLE because of some infinite recursion.
I solved with the following code :
class Solution {
private:
int d[4][2] = {{0,1}, {0,-1}, {1,0}, {-1,0}};
bool within_bound(int i, int j, int n){
return (i>=0 && i=0 && j
Bhaii kyaa intuition smjhate ho ek bar me dimag ke andar ..main aapke playlist se hi graph padh rha hau ❤❤❤
Wow. You made my day.
Thank you so much Arif ❤️❤️❤️
Bhai kaha se hain aap main really kabhi na kabhi apse milna chahta hu..😛mjhe graph padhna tha par apke jaisa koi nhi padhta phr mjhe apka channel milaa to leetcode challenge shuru Kiya aur graphs bhii..love you bhaii may Allah bless you keep shining 🙏🙏
Thanks a lot Arif.
I am just a normal guy like you all.
I am also learning and growing with you all
codestorywithMIK is my goto channel for daily streak questions. I wait for you to upload the video instead of watching someone else's video as here we learn to build the intuition. Thank you Sir ❤
Thank you so much. It means a lot to me
Your explanations are so clear and easy to understand. Thank you for making this video!🤝
Thank you so much ❤️❤️
best explanation on youtube
Thank you so much ❤️😇🙏🙏
Your teaching skills are on the next level 😍
Thanks a lot Anup ❤️❤️
As always again you are amazing sir🔥, please please make video on leetcode contest upsolving i am eagerly waiting for this series because after giving contest i can't find any solid solutions for those problems which i was unable to solve , it will really helpful for every leetcoders whom giving contest seriously , it's a huge request sir🙏🙏
Thanks a lot Ashutosh.
I understand and will definitely plan to cover contests soon.
Currently a little occupied because of DP concepts playlist
But for sure soon
@@codestorywithMIK thanks 🙏
Thank you KING of Graphs 👑👑 Java implementation:
PS : Code might look lengthy because I have overridden equals and hashCode method to use Pair class in set. But this would work for any future code where you need this conversion as well.
class Pair {
int row;
int col;
Pair(int _row, int _col) {
row = _row;
col = _col;
}
@Override
public int hashCode() {
int hash = 3;
hash = 47 * hash + this.row;
hash = 47 * hash + this.col;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pair other = (Pair) obj;
if (this.row != other.row) {
return false;
}
if (this.col != other.col) {
return false;
}
return true;
}
}
class Solution {
private int m;
private int n;
private int[][] directions = {
{-1,0},
{0,-1}, {0,1},
{1,0}
};
private boolean isSafe(int i, int j) {
if(i=m || j=n){
return false;
}
return true;
}
private void dfs(int i, int j, int[][] grid, Set visitedCells) {
if(!isSafe(i,j) || grid[i][j] == 0 || visitedCells.contains(new Pair(i,j))) {
return;
}
visitedCells.add(new Pair(i,j));
for(int[] dir : directions) {
int i_ = i + dir[0];
int j_ = j + dir[1];
dfs(i_,j_,grid,visitedCells);
}
}
private int bfs(int[][] grid, Set visitedCells) {
Queue q = new LinkedList();
for(Pair p : visitedCells) {
q.offer(p);
}
int level = 0;
while(!q.isEmpty()) {
int L = q.size();
while(L-- > 0) {
Pair pair = q.poll();
int x = pair.row;
int y = pair.col;
for(int[] dir : directions) {
int x_ = x + dir[0];
int y_ = y + dir[1];
Pair p_ = new Pair(x_,y_);
if(isSafe(x_,y_) && !visitedCells.contains(p_)) {
if(grid[x_][y_] == 1) return level;
visitedCells.add(p_);
q.offer(p_);
}
}
}
level++;
}
return level;
}
public int shortestBridge(int[][] grid) {
this.m = grid.length;
this.n = grid[0].length;
Set visitedCells = new HashSet();
for(int i=0; i
Thanks a lot JJ
Yaar your JAVA is 🔥🔥🔥.
Thanks a lot
King 👑
One of the best explanations
First I did a brute force approach. I stored all cells of first island by dfs. And then store the cells of second island and find the Manhattan distance for every combination of both islands. This got TLE. Then did multi source BFS until i got a 1 from second island.. got accepted
But your approach will not get TLE , I have made submission exactly like your brute force
Good explanation 👌
Could you please make a video of today's daily challenge like how to solve it with the help of bucket sort because solving it with the help of priority queue is simple and understandable but there are very less videos explaning how to use bucket sort for this question.
Sure thing Mandeep ❤️❤️❤️
Uploaded. I hope it helps ❤️
@@codestorywithMIK Thank you sir
excellent video
Thanks a lot Sneha
Hi mik , i completed graph concept playlist . just a request can you make vdeo on how to find cut vertex and cut edge ie bridges in graph . It will helpful for sem exam too .
WHY DON'T YOU EVER TELL US IN YOUR VIDEO TO SUBSCRIBE, LIKE or SHARE your contents.
WHY ?????
Janta jawab chahti hai
Bro this is speciality of this channel. Subscribers yah par bol kar nahi aate balki khud chale aate hai.😀
@@ajit287 True bro
Without using visited array it is even more fast means no need of visited array here
Bhaiya thanks for this awesome explanation .Can you please suggest follow up for the same ?
Thanks Vineet.
If you click on my Github code link in the description, there are related problems for this Qn, you should definitely give then a try
Here's the Java code
Tip: Java has a inbuilt Pair already, no need to make a class like others keep doing
class Solution {
int n, m;
int[][] directions = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
public boolean inBorder(int[][] grid, int i, int j) {
return i >= 0 && j >= 0 && j < m && i < n;
}
public void dfs(int[][] grid, int i, int j, Set visited) {
if (!inBorder(grid, i, j) || visited.contains(new Pair(i, j)) || grid[i][j] == 0) return;
visited.add(new Pair(i, j));
for (int[] dir : directions) {
int _x = i + dir[0];
int _y = j + dir[1];
dfs(grid, _x, _y, visited);
}
}
public int bfs(int[][] grid, int i, int j, Set visited) {
Queue queue = new LinkedList();
for (Pair p : visited) {
queue.add(p);
}
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
Pair p = queue.poll();
int first = p.getKey();
int second = p.getValue();
for (int[] dir : directions) {
int _x = first + dir[0];
int _y = second + dir[1];
if (inBorder(grid, _x, _y) && !visited.contains(new Pair(_x, _y))) {
if (grid[_x][_y] == 1) {
return level;
}
visited.add(new Pair(_x, _y));
queue.add(new Pair(_x, _y));
}
}
}
level++;
}
return level;
}
public int shortestBridge(int[][] grid) {
n = grid.length;
m = grid[0].length;
Set visited = new HashSet();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 1) {
dfs(grid, i, j, visited);
return bfs(grid, i, j, visited);
}
}
}
return -1;
}
}
Wow
Masterpiece
❤
Apka streak kaise reset ho gya ??
I think , I had already made a video on Bipartite Graph long back. So i missed to solve it for streak this time.
But dont worry, i will cover it with my coin 😁
I first got tle....
Can you share your code ?
@@codestorywithMIK I found my redundant work. Thank you...
Great 😊❤️