TIMESTAMPS ---------------------------------------------------------------------------------------------------------------- 00:00:00 - problem overview 00:01:39 - algorithm walk through 00:10:40 - code walk through 00:16:36 - time and space complexity Enjoy :)
@tofahub2 жыл бұрын
This channel is so underrated
@phanichoragudi574 жыл бұрын
Much cleaner code than what I have seen out there. Subscribed immediately!
@AlgosWithMichael4 жыл бұрын
Awesome to hear, thank you for the support!
@bazzalseed4 жыл бұрын
How is the time complexity O(M*N)? If every cell has gold, you will have to do DFS from each single position. That will yield a O(M^2N^2) run time right?
@AlgosWithMichael4 жыл бұрын
I think you are right, in the scenario we have gold at every position our DFS would visit all cells. Good catch!
@nicholase28684 жыл бұрын
i was thinking the same thing. M*N for the top level iteration, then M*N for the DFS, so (M*N)^2.
@ishitachausalkar15673 жыл бұрын
@@AlgosWithMichael but chances of that is slim as the number of rows and columns can be 15 x 15 = 225, but they have said that at-most 25 cells can have gold.
@codingessential913 жыл бұрын
since this problem does not have similar subproblems (as max gold from any index depends on from where we are coming), hence we cannot memoize this solution. that is why this is sort of "unoptimised dp" and has exponential time.
@serhattural31682 жыл бұрын
It should be k*(3^k) where k is m*n. We trying to find start point by itareting on the grid(m*n) times. For each start point we have 3 possible direction (exclude prev cell). And the depth can be all grid cells(m*n). this is like starting point grid(1,1) -> [grid(2,1), grid(1,2), grid(0,1), grid(1,0)] repeat this for all childs again.
@yitingg79424 жыл бұрын
hahaha again two mins into watching your video, I fixed my code and got it right. It shows that a great teacher is someone who inspires the students and enlightens them lol. Thank you so much!
@AlgosWithMichael4 жыл бұрын
Glad I could help!
@nitisharma89734 жыл бұрын
Michael, you explained it so lucidly. Subscribed and looking forward to see more content like this from you.
@AlgosWithMichael4 жыл бұрын
Thank you so much for the support!
@SelftaughtSoftwareEngineer4 жыл бұрын
Your explanations are always so clear and easy to understand. Thank you!
@AlgosWithMichael4 жыл бұрын
You're very welcome!
@sukhrobgolibboev4 жыл бұрын
your explanations are great using the "whiteboard", keep up the great work
@AlgosWithMichael4 жыл бұрын
Awesome, thanks for watching!
@rajat15484 жыл бұрын
Great code and a very simple explanation. I immediately subscribed it.
@AlgosWithMichael4 жыл бұрын
Awesome, thank you!
@iamryuzaki3 жыл бұрын
The time complexity can be reduced from being exponential to N*M if you visit one node only once by making it visited permanently or change gold to zero in the original matrix after visiting it.
@gauravkungwani2 жыл бұрын
try doing it, you will get wrong ans. You haven't understood it well.
@YashTrivedi213 жыл бұрын
Thanks for the explanation. I was thinking of some optimization don't know if it would work. What if we make another 2d array of same size and store the Max value for each cell in all 4 directions? So when we visit that cell again we don't have traverse again.
@rosonerri-faithful3 жыл бұрын
Great intuition and solution,buddy..keep it up. Thanks for the video
@chandragirivishnuvardhan76544 жыл бұрын
Awesome explanation.Thank you so much for your valuable and catchy explanation.
@AlgosWithMichael4 жыл бұрын
You are most welcome
@ruchikas0053 жыл бұрын
I recently find out your channel, and it is super amazing. Well done and kudos :-)
@AlgosWithMichael3 жыл бұрын
Thank you very much!
@hotspur2174 жыл бұрын
Solid explanation mate. Keep doing more videos.
@AlgosWithMichael4 жыл бұрын
More to come!
@fangyizhu88934 жыл бұрын
Very clear and easy to understand. Thank you!
@AlgosWithMichael4 жыл бұрын
No problem, thanks for watching!
@ankit_irl3 жыл бұрын
Clean implementation. Thank you for this
@AlgosWithMichael3 жыл бұрын
Of course, anytime
@biswadeepchakraborty35463 жыл бұрын
Hi could you please make a tutorial on this kind of problems from the scratch like how to use this dfs along with these backtracking approach. I mean could you dry run and make a tutorial on this please
@sivahacker88574 жыл бұрын
Thank you so much. We want graphs like that.
@AlgosWithMichael4 жыл бұрын
Anytime!
@satyanarayanmohanty34154 жыл бұрын
Brilliant explanation as always.
@AlgosWithMichael4 жыл бұрын
Glad you liked it!
@ayushman_sr2 ай бұрын
etf did you did with T.C it will be (r*c)^2 or n^4 ish
@kumarc48533 жыл бұрын
Hey @Michael , Thanks for the video. Question: if there is a new boolean array being passed into the DFS function, is the backtracking on visited cells needed?
@shikharchaudhary8353 жыл бұрын
Hey, good explanation, but just one doubt, shouldnt the time complexity be - m*n*3^mn ?
@jinzituo3 жыл бұрын
Time complexity should be exponential since this is a classical backtracking problem.
@vinprabhu4 жыл бұрын
You deserve more views
@AlgosWithMichael4 жыл бұрын
Thank you kindly
@mardhapuhareesh45534 жыл бұрын
Super bro no words to say anything 💯
@AlgosWithMichael4 жыл бұрын
Haha awesome man thanks
@AbhishekSingh-og7kf2 жыл бұрын
Thanks Michael.🔥
@_ankit_4 жыл бұрын
Heyy Michael I was trying to solve same problem and was stuck but then referred to your video and it worked. But I still have some doubts regarding the approach. If I am not writing this last line visited[i][j]=false; then I am getting TLE error, idk why it is happening.
@AlgosWithMichael4 жыл бұрын
It's because we need to re-use the position at a later time in our DFS. If we don't set the position marked as true back to false when we finish searching in the path, we may not get the correct solution. In other words, a position can be used for multiple paths in the DFS.
@_ankit_4 жыл бұрын
@@AlgosWithMichael no, I m getting correct answer since I didn't pass visited array as reference so when we will back to previos call of function in function call stack the visited array will be at previous state doing same thing as adding that line in your code. But idk why am I getting a TLE error when I submit it.
@saumyachoudhary8633 жыл бұрын
@@_ankit_ same i am also getting tle, even I am passing visited with reference and doing visited[i][j]=false and it runs fine if we run that test case while compile and run but when I am going to submit it shows tle. did anyone find the solution to this problem?
@saumyachoudhary8633 жыл бұрын
@@AlgosWithMichael same i am also getting tle, even I am passing visited with reference and doing visited[i][j]=false and it runs fine if we run that test case while compile and run but when I am going to submit it shows tle. did anyone find the solution to this problem?
@munchika70134 жыл бұрын
you should add grid[i][j] in line 27 before returning
@AlgosWithMichael4 жыл бұрын
i dont think that is necessary though since we just need to return the max from the directions
@curesnow64932 жыл бұрын
Good explanation! I am still confused about the visited 2d Array, like mark true if visited, then mark false when it is done on the current cell. Can you please explain it to me? Thanks!
@AlgosWithMichael2 жыл бұрын
Sure! When we mark the position as true, we are saying that this position has been seen on this path. When we mark the position as false, we are doing backtracking because we are pretty much telling our algorithm that we can now used this previously marked true position that it can be used again in another path. Hope that makes sense!
@curesnow64932 жыл бұрын
@@AlgosWithMichael thanks for your explanation !
@prithvirajan1055 ай бұрын
Thanks... from india..🙌
@guymograbi Жыл бұрын
Checking if i,j cell holds a 0 will make you miss some solutions.
@munchika70134 жыл бұрын
Where do you return grid value? This will always return 0
@AlgosWithMichael4 жыл бұрын
I return the maximum of all the directions at the end of the recursive function. That is technically the grid value. Thanks for watching!
@munchika70134 жыл бұрын
@@AlgosWithMichael I tried your exact code and it returns zero for me.
@ishq_gunah_hai31794 жыл бұрын
@@munchika7013 Check the variable declaration i.e. public and private. Get stronger with oops concept. If still you cannot figure out contact me.
@TheElementFive2 жыл бұрын
Returns zero for me as well
@parthkolgiri7501 Жыл бұрын
Damn, you are smart!
@natalieweishuhn5213 жыл бұрын
Something's not working right if translated into python. What did I do wrong here? def getMaximumGold(self, grid): """ :type grid: List[List[int]] :rtype: int """ if grid == None or len(grid) == 0: return 0 maxTotal = 0 m = len(grid) n = len(grid[0]) def dfs(grid, i, j, m, n, visited): #rtype: int #out of bounds return 0 #visited[i][j] == True return 0 #grid[i][j] == 0 return 0 if (i < 0) or (j < 0) or (i >= m) or (j >= n) or (grid[i][j] == 0) or (visited[i][j]): return 0 #start visiting visited[i][j] = True left = dfs(grid, i, j-1, m, n, visited) right = dfs(grid, i, j+1, m, n, visited) up = dfs(grid, i-1, j, m, n, visited) down = dfs(grid, i+1, j, m, n, visited) #done visiting visited[i][j] = False return (max(left, right, up, down)) + grid[i][j] for i in range(m): for j in range(n): visited = [[False]*n]*m if grid[i][j] != 0: gold = dfs(grid, i, j, m, n, visited) maxTotal = max(maxTotal, gold) return maxTotal
@aasthajain49873 жыл бұрын
great explaination!!
@AlgosWithMichael3 жыл бұрын
Thank you!
@chaoschao94324 жыл бұрын
very helpful, thanks!
@AlgosWithMichael4 жыл бұрын
Glad to hear, thanks for watching!
@atharvakango72444 жыл бұрын
Clean! Awesome!
@AlgosWithMichael4 жыл бұрын
Thanks!
@biaojin51884 жыл бұрын
I have not see the running result. can you run it ?
@AlgosWithMichael4 жыл бұрын
I run the code at 16:28, is that what you mean?
@jagrit074 жыл бұрын
Thanks, man.
@AlgosWithMichael4 жыл бұрын
No problem 👍
@himanshuchhikara49184 жыл бұрын
super amazing explanation sir
@AlgosWithMichael4 жыл бұрын
Thank you!
@anands94074 жыл бұрын
hey can u tell me when do i know when do i know it is a backtracking problem
@anands94074 жыл бұрын
btw this was the best explanation of a backtracking problem i've ever seen...keep making videos!even if noone watches i will :P
@AlgosWithMichael4 жыл бұрын
I can tell to use backtracking when the problem could have many solutions and at each state you determine if you are on the appropriate path or not. You repeat this process until you get to your goal state. Hope that makes sense!
@anands94074 жыл бұрын
@@AlgosWithMichael yes..but still it is pretty similar to dp right...very often i use dp on backtracking problems and vice versa..how do i distinguish between them
@AlgosWithMichael4 жыл бұрын
Yea, there are many problems that involve the use of backtracking, but in addition to that you can use memoization (dp) to solve the problem more effeciently. I suppose those two things go hand and hand, but there are times when cases do not repeat themselves and backtracking must be done without dp.
@anands94074 жыл бұрын
thanks for the help :)
@katielamber02382 жыл бұрын
man I love you ahahah
@AlgosWithMichael2 жыл бұрын
Thank you!
@aishwaryam10524 жыл бұрын
Why do we set visited[i][j]=false again?
@AlgosWithMichael4 жыл бұрын
To allow for that specific cell to be used in another calculation as the algorithm moves forward.
@aishwaryam10524 жыл бұрын
@@AlgosWithMichael Thank you!
@ishq_gunah_hai31794 жыл бұрын
@@AlgosWithMichael Awesome man 💗
@AlgosWithMichael4 жыл бұрын
Thanks!
@AlgosWithMichael4 жыл бұрын
Anytime!
@TheElementFive2 жыл бұрын
Returns 0 for me..
@rishavsaha52542 жыл бұрын
int dfs(vector&grid,int n,int m,int i,int j) { if(i=n || j=m || grid[i][j]==0) return 0; int val=grid[i][j]; grid[i][j]=0; //So that we cannot again move to this index in this DFS call int a=val+dfs(grid,n,m,i-1,j); int b=val+dfs(grid,n,m,i,j-1); int c=val+dfs(grid,n,m,i+1,j); int d=val+dfs(grid,n,m,i,j+1); grid[i][j]=val; //Backtracking return max(max(a,b),max(c,d)); } int getMaximumGold(vector& grid) { int n=grid.size(),m=grid[0].size(); int maxGold=0; for(int i=0;i
@shishirarora88087 ай бұрын
O(m * n * 4^(m * n))
@manasbolt4 жыл бұрын
why are we doing visited[i][j]=false at the end of the function??
@AlgosWithMichael4 жыл бұрын
So that we can re-use that position at a later time in our DFS. If we don't set it back to false, it won't be used again.