Nice , I follow your course from 3-4month before from this Feb 21/2022 and honestly I can understand how to approach and how to think such type of questions ...... Maza hi rha hai but pure tarike se abhi Sikh rha hu... Thanku luv 💯💯
Try to solve island perimeter question from leetcode after understanding this problem where you have to take care of boundaries of your traversal only---- i tried by myself just click me the approach and i submitted successfully.
@@Yuvraj20011 int cnt=0; void dfs(int i , int j, vector&graph) { int n=graph.size(); int m= graph[0].size(); if(i=m)return; if(graph[i][j]!=1)return; if(i-1=m)cnt++; if(i-1>=0 && graph[i-1][j]==0)cnt++;//one side water if(j-1>=0 && graph[i][j-1]==0)cnt++;//one side water if(i+1
We haven't used the visited as visited is used to prevent the initial recursion calls, but in this we are changing the values of image itself that will do the same work as visited and helps in prevention of initial recursion to be get called again. Edited for second question on every dfs calls just do count++ ..... And return the count.
💛 understood 👍 I tried the visited array technique for second question (no.of islands) it worked fine with some little tweks of previous question code👍
in dfs , generally after every recursion call, the state of the final result keeps changing.in order to maintain the state , we backtrack. Here there is no need once we change the color . So visited array is not used
In the flood fill question, it is not required to use the visited thing as we are already using the color as a sign of vis. So it would be not useful for using the visited thing separately while performing dfs.
I thought of this too, but I am not sure whether this is right or not. Could you drop your solution here with this approach so I could take a look. Thanks
visited not required because just after checking whether the cell is changeable we changed its color and so if in the next case it will return as it will not meet the case color[i][j] != inicolor
Bfs is used in very specific cases only which I will explain in future videos; rest must of the stuff done using dis can be done using bfs as well; so mostly you will always use dis only until and unless any special case arrives where only bfs works..
I was able to solve the no of island question with your trainings. Thanks a lot class Solution { public static boolean[][] visited; void DFS(int i, int j, int m, int n, char[][] grid) { if (i < 0 || j < 0) return; if (i >= m || j >= n) return; if (visited[i][j]) return; if (grid[i][j] == '0') return; visited[i][j] = true; DFS(i, j - 1, m, n, grid); DFS(i, j + 1, m, n, grid); DFS(i - 1, j, m, n, grid); DFS(i + 1, j, m, n, grid); } public int numIslands(char[][] grid) { int noi = 0; int m = grid.length; int n = grid[0].length; visited = new boolean[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] != '0' && !visited[i][j]) { noi++; DFS(i, j, m, n, grid); } } } return noi; } }
Solved the number of islands question after watching your video but its showing seg fault can anyone help? The code void dfs(int i,int j,vector& grid,vector &vis) { int r=grid.size(); int c=grid[0].size(); if(ic) return; if(grid[i][j]=='0') return; if(vis[i][j]) return; vis[i][j]=true; dfs(i+1,j,grid,vis); dfs(i-1,j,grid,vis); dfs(i,j+1,grid,vis); dfs(i,j-1,grid,vis); dfs(i+1,j+1,grid,vis); dfs(i+1,j-1,grid,vis); dfs(i-1,j+1,grid,vis); dfs(i-1,j-1,grid,vis); } int numIslands(vector& grid) { // Code here vector vis; int cnt=0; for(int i=0;i
Find my solution below class Solution { public: void dfs(int i, int j, int n, int m, vector &vis, vector &grid) { //condition checks on the entering node if(i=m) return;
if(grid[i][j]=='0') { vis[i][j]=1; return; } if(vis[i][j]==1) return; //operation on current node vis[i][j]=1; //apply dfs on the child nodes on all four directions ' dfs(i,j+1,n,m,vis,grid); dfs(i,j-1,n,m,vis,grid); dfs(i+1,j,n,m,vis,grid); dfs(i-1,j,n,m,vis,grid); } int numIslands(vector& grid) { //cout
💛 Bro iss code kya problem hai class Solution { public: void dfs(int i,int j,vector& grid){ int n=grid.size(); int m=grid[0].size(); if(i=m) return; if(grid[i][j]!='1') return; dfs(i-1,j,grid); dfs(i+1,j,grid); dfs(i,j-1,grid); dfs(i,j+1,grid);
} int numIslands(vector& grid) { int counter=0; for(int i=0;i
Nice ,
I follow your course from 3-4month before from this Feb 21/2022 and honestly I can understand how to approach and how to think such type of questions ......
Maza hi rha hai but pure tarike se abhi Sikh rha hu...
Thanku luv 💯💯
This is whats makes u different and unique from others 8:53 , thanks man i got what i came for
💛 loved it, can't be explained more beautifully!!
Try to solve island perimeter question from leetcode after understanding this problem where you have to take care of boundaries of your traversal only---- i tried by myself just click me the approach and i submitted successfully.
bhai code bhejdo mujhe error aa rha hai shayad recursion wale part me
@@Yuvraj20011
int cnt=0;
void dfs(int i , int j, vector&graph)
{
int n=graph.size();
int m= graph[0].size();
if(i=m)return;
if(graph[i][j]!=1)return;
if(i-1=m)cnt++;
if(i-1>=0 && graph[i-1][j]==0)cnt++;//one side water
if(j-1>=0 && graph[i][j-1]==0)cnt++;//one side water
if(i+1
@@Yuvraj20011 class Solution {
public:
int dfs(int i, int j, vector& grid, vector & vis){
int count = 0;
int n = grid.size();
int m= grid[0].size();
if(i=m){
return 1;
}
if(grid[i][j] == 0){
return 1;
}
if(vis[i][j])return 0;
vis[i][j] = 1;
count += dfs(i+1, j, grid, vis);
count += dfs(i-1, j, grid, vis);
count += dfs(i, j+1, grid, vis);
count += dfs(i, j-1, grid, vis);
return count;
}
int islandPerimeter(vector& grid) {
vector vis;
int n = grid.size();
int m= grid[0].size();
for(int i = 0; i
We haven't used the visited as visited is used to prevent the initial recursion calls, but in this we are changing the values of image itself that will do the same work as visited and helps in prevention of initial recursion to be get called again.
Edited for second question on every dfs calls just do count++ ..... And return the count.
maybe u have nt tried to submit the code.
[[0,0,0],[0,0,0]]
0
0
0 try this case. it wont work without visited array.
@@ENGCS_JaiSaxena In main loop, we already declared that if new color equals initial color, we wont run dfs, so no use of visited array.
wah luv sir, island vala khud kar liya , thank you bhaiya
I will always be very very grateful to u bhai!! Ur explanation is really great.
💛 understood 👍
I tried the visited array technique for second question (no.of islands) it worked fine with some little tweks of previous question code👍
Your videos are too good ! Awesome !!
@Luv . Pls make some playlist on Leetcode Daily Challenges. It will be very much helpful for the Interview preparation for Everyone. Thanks
💛 sir learners missing u ....great explanation
Jahapana tusi great ho , taufa kabool karo 🙇🏻🙇🏻
thank you so so much..ab jaake cheeze smjh aarahi h
in dfs , generally after every recursion call, the state of the final result keeps changing.in order to maintain the state , we backtrack. Here there is no need once we change the color . So visited array is not used
Love you brother 😚
Your each video makes me happy 🙂
Very well explained, keep growing brother!! 💛
13:55 Visited not used because color will already be changes and then if It will go back then it would no longer be a valid edge.
💛 mind blowing explanation!!!
💛 Excellent Explanation
Amazing explanation!!
Explained so well! 👏
Coding with Luv is Love 💙💙💙
💛 Loved it
💛 luv your content
Explained so well
Thanks for such a great explanation.
Great Explanation💛💛
Best explaination :)
i didnt find yellow heart bt i watch ur vdo completely
Wow I got a heart first time😱😱
Thank you so bhaiya for your efforts 🥰
very good lecture
you are making it damn easy
wonderful explanation;
First comment ❤️
awesome explanation
Very interesting 💚💚
Love you bhaiya 💗
bro i think u should make a video on how to traverse a 2d graph and find some path with dfs. this topic seems quite hard for us.
Thank you 💛
Bhaiya i just thought about checking the condition of being initial color equal to new color and get the idea why visited is not used ....
visited array may not be required because we do coloring if it is colored with new color then dfs not called
thank you bhaiya for this playlist
island perimeter question which is mentioned in the description is not a graph question....right?
In the flood fill question, it is not required to use the visited thing as we are already using the color as a sign of vis. So it would be not useful for using the visited thing separately while performing dfs.
I thought of this too, but I am not sure whether this is right or not. Could you drop your solution here with this approach so I could take a look. Thanks
@@lakshyasharma7448 so you need the sol, huh
Yellow heart 👍👍
visited not required because just after checking whether the cell is changeable we changed its color and so if in the next case it will return as it will not meet the case color[i][j] != inicolor
very nice
💛 thanks bhaiyya
Luv Sir Op 🥳
🐺 🤝 Thankx Bhai !
How do we knw when to use DFS and when to use BFS coz sometimes its confusing??
Bfs is used in very specific cases only which I will explain in future videos; rest must of the stuff done using dis can be done using bfs as well; so mostly you will always use dis only until and unless any special case arrives where only bfs works..
@@iamluv Thanks a lot
BFS is usually better than dfs because it does not involve recursive calls and explores level by level
is this same algorithm used in Chain reaction game??
Best❤️
Thanks Bhaiya
wow🔥🔥
💛 for you
💛 doneee
❤Thank You❤
Sir please Explain Covishield vs Covaxsin Codechef Problem
Bro, could you please tell how can we avoid the use of visited array in undirected graphs ?
in graphs you cannot, in trees you can
@@iamluv Will you tell in your upcoming videos how to do so like you said ?
I was able to solve the no of island question with your trainings. Thanks a lot
class Solution {
public static boolean[][] visited;
void DFS(int i, int j, int m, int n, char[][] grid) {
if (i < 0 || j < 0) return;
if (i >= m || j >= n) return;
if (visited[i][j]) return;
if (grid[i][j] == '0') return;
visited[i][j] = true;
DFS(i, j - 1, m, n, grid);
DFS(i, j + 1, m, n, grid);
DFS(i - 1, j, m, n, grid);
DFS(i + 1, j, m, n, grid);
}
public int numIslands(char[][] grid) {
int noi = 0;
int m = grid.length;
int n = grid[0].length;
visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != '0' && !visited[i][j]) {
noi++;
DFS(i, j, m, n, grid);
}
}
}
return noi;
}
}
mast
sorry bhaiya video dekhne m thora late ho gaya ❤
Some companies will ask you why did you use dfs instead of bfs? What to answer here?
Solved the number of islands question after watching your video but its showing seg fault can anyone help?
The code
void dfs(int i,int j,vector& grid,vector &vis)
{
int r=grid.size();
int c=grid[0].size();
if(ic) return;
if(grid[i][j]=='0') return;
if(vis[i][j]) return;
vis[i][j]=true;
dfs(i+1,j,grid,vis);
dfs(i-1,j,grid,vis);
dfs(i,j+1,grid,vis);
dfs(i,j-1,grid,vis);
dfs(i+1,j+1,grid,vis);
dfs(i+1,j-1,grid,vis);
dfs(i-1,j+1,grid,vis);
dfs(i-1,j-1,grid,vis);
}
int numIslands(vector& grid) {
// Code here
vector vis;
int cnt=0;
for(int i=0;i
How to do if it is diagonally visiting?
flood fill problem is also known as the rotten tomatoes problem , right?
Tak nice
💛💛💛💛
💛💛💛
8-aug 2022
Find my solution below
class Solution {
public:
void dfs(int i, int j, int n, int m, vector &vis, vector &grid)
{
//condition checks on the entering node
if(i=m) return;
if(grid[i][j]=='0')
{
vis[i][j]=1;
return;
}
if(vis[i][j]==1) return;
//operation on current node
vis[i][j]=1;
//apply dfs on the child nodes on all four directions '
dfs(i,j+1,n,m,vis,grid);
dfs(i,j-1,n,m,vis,grid);
dfs(i+1,j,n,m,vis,grid);
dfs(i-1,j,n,m,vis,grid);
}
int numIslands(vector& grid)
{
//cout
class Solution {
public:
void dfs(int i,int j,vector& grid){
int n=grid.size();
int m=grid[0].size();
if(i=m)return;
if(grid[i][j]!='1')return;
grid[i][j]='0';
dfs(i-1,j,grid);
dfs(i+1,j,grid);
dfs(i,j-1,grid);
dfs(i,j+1,grid);
}
int numIslands(vector& grid) {
int cnt=0;
for(int i=0;i
Red heart chhodenge ❤️
Background music name
good explanation, but this code wont run for
[[0,0,0],[0,0,0]]
0
0
0
it will enter , infinite loop so we have to use visited.
Bro, he has already included a condition in main function, where initial color is equal to final color. So we'll not run dfs for this case.
Then solve the above test case bro it wont get pass.
Just dont say without even submitting the problem
@@ENGCS_JaiSaxena do u get the answer?
@@arpitgupta2358 what??
@@ENGCS_JaiSaxena [[0,0,0],[0,0,0]] iss test case ke liye aagya answer tumhara?
💛
Bhaiya main function ka code q nhi dikhate???
6:53
💛
Bro iss code kya problem hai
class Solution {
public:
void dfs(int i,int j,vector& grid){
int n=grid.size();
int m=grid[0].size();
if(i=m) return;
if(grid[i][j]!='1') return;
dfs(i-1,j,grid);
dfs(i+1,j,grid);
dfs(i,j-1,grid);
dfs(i,j+1,grid);
}
int numIslands(vector& grid) {
int counter=0;
for(int i=0;i
set grid[i][j] = '0' too after if() statements in dfs function
this way you won't go back to the nodes which you have already visited
♥️
Bhaiya Leetcode p 400 questions kr liye ab Elon Musk ko hara skta hu 🙄
💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛
Yellow heart
change color to yellow
💛
💛
💛💛
💛💛
💛
💛💛💛
💛💛
💛
💛💛
💛
💛
💛💛💛
💛
💛