Google Coding Interview Question - Path With Maximum Gold (LeetCode)

  Рет қаралды 33,503

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 103
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
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 :)
@tofahub
@tofahub 2 жыл бұрын
This channel is so underrated
@phanichoragudi57
@phanichoragudi57 4 жыл бұрын
Much cleaner code than what I have seen out there. Subscribed immediately!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Awesome to hear, thank you for the support!
@bazzalseed
@bazzalseed 4 жыл бұрын
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?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I think you are right, in the scenario we have gold at every position our DFS would visit all cells. Good catch!
@nicholase2868
@nicholase2868 4 жыл бұрын
i was thinking the same thing. M*N for the top level iteration, then M*N for the DFS, so (M*N)^2.
@ishitachausalkar1567
@ishitachausalkar1567 3 жыл бұрын
@@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.
@codingessential91
@codingessential91 3 жыл бұрын
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.
@serhattural3168
@serhattural3168 2 жыл бұрын
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.
@yitingg7942
@yitingg7942 4 жыл бұрын
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!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad I could help!
@nitisharma8973
@nitisharma8973 4 жыл бұрын
Michael, you explained it so lucidly. Subscribed and looking forward to see more content like this from you.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you so much for the support!
@SelftaughtSoftwareEngineer
@SelftaughtSoftwareEngineer 4 жыл бұрын
Your explanations are always so clear and easy to understand. Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
You're very welcome!
@sukhrobgolibboev
@sukhrobgolibboev 4 жыл бұрын
your explanations are great using the "whiteboard", keep up the great work
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Awesome, thanks for watching!
@rajat1548
@rajat1548 4 жыл бұрын
Great code and a very simple explanation. I immediately subscribed it.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Awesome, thank you!
@iamryuzaki
@iamryuzaki 3 жыл бұрын
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.
@gauravkungwani
@gauravkungwani 2 жыл бұрын
try doing it, you will get wrong ans. You haven't understood it well.
@YashTrivedi21
@YashTrivedi21 3 жыл бұрын
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-faithful
@rosonerri-faithful 3 жыл бұрын
Great intuition and solution,buddy..keep it up. Thanks for the video
@chandragirivishnuvardhan7654
@chandragirivishnuvardhan7654 4 жыл бұрын
Awesome explanation.Thank you so much for your valuable and catchy explanation.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
You are most welcome
@ruchikas005
@ruchikas005 3 жыл бұрын
I recently find out your channel, and it is super amazing. Well done and kudos :-)
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you very much!
@hotspur217
@hotspur217 4 жыл бұрын
Solid explanation mate. Keep doing more videos.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
More to come!
@fangyizhu8893
@fangyizhu8893 4 жыл бұрын
Very clear and easy to understand. Thank you!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem, thanks for watching!
@ankit_irl
@ankit_irl 3 жыл бұрын
Clean implementation. Thank you for this
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Of course, anytime
@biswadeepchakraborty3546
@biswadeepchakraborty3546 3 жыл бұрын
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
@sivahacker8857
@sivahacker8857 4 жыл бұрын
Thank you so much. We want graphs like that.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Anytime!
@satyanarayanmohanty3415
@satyanarayanmohanty3415 4 жыл бұрын
Brilliant explanation as always.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad you liked it!
@ayushman_sr
@ayushman_sr 2 ай бұрын
etf did you did with T.C it will be (r*c)^2 or n^4 ish
@kumarc4853
@kumarc4853 3 жыл бұрын
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?
@shikharchaudhary835
@shikharchaudhary835 3 жыл бұрын
Hey, good explanation, but just one doubt, shouldnt the time complexity be - m*n*3^mn ?
@jinzituo
@jinzituo 3 жыл бұрын
Time complexity should be exponential since this is a classical backtracking problem.
@vinprabhu
@vinprabhu 4 жыл бұрын
You deserve more views
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you kindly
@mardhapuhareesh4553
@mardhapuhareesh4553 4 жыл бұрын
Super bro no words to say anything 💯
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Haha awesome man thanks
@AbhishekSingh-og7kf
@AbhishekSingh-og7kf 2 жыл бұрын
Thanks Michael.🔥
@_ankit_
@_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.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
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_
@_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.
@saumyachoudhary863
@saumyachoudhary863 3 жыл бұрын
@@_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?
@saumyachoudhary863
@saumyachoudhary863 3 жыл бұрын
​@@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?
@munchika7013
@munchika7013 4 жыл бұрын
you should add grid[i][j] in line 27 before returning
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
i dont think that is necessary though since we just need to return the max from the directions
@curesnow6493
@curesnow6493 2 жыл бұрын
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!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
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!
@curesnow6493
@curesnow6493 2 жыл бұрын
@@AlgosWithMichael thanks for your explanation !
@prithvirajan105
@prithvirajan105 5 ай бұрын
Thanks... from india..🙌
@guymograbi
@guymograbi Жыл бұрын
Checking if i,j cell holds a 0 will make you miss some solutions.
@munchika7013
@munchika7013 4 жыл бұрын
Where do you return grid value? This will always return 0
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I return the maximum of all the directions at the end of the recursive function. That is technically the grid value. Thanks for watching!
@munchika7013
@munchika7013 4 жыл бұрын
@@AlgosWithMichael I tried your exact code and it returns zero for me.
@ishq_gunah_hai3179
@ishq_gunah_hai3179 4 жыл бұрын
@@munchika7013 Check the variable declaration i.e. public and private. Get stronger with oops concept. If still you cannot figure out contact me.
@TheElementFive
@TheElementFive 2 жыл бұрын
Returns zero for me as well
@parthkolgiri7501
@parthkolgiri7501 Жыл бұрын
Damn, you are smart!
@natalieweishuhn521
@natalieweishuhn521 3 жыл бұрын
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
@aasthajain4987
@aasthajain4987 3 жыл бұрын
great explaination!!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@chaoschao9432
@chaoschao9432 4 жыл бұрын
very helpful, thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Glad to hear, thanks for watching!
@atharvakango7244
@atharvakango7244 4 жыл бұрын
Clean! Awesome!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks!
@biaojin5188
@biaojin5188 4 жыл бұрын
I have not see the running result. can you run it ?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
I run the code at 16:28, is that what you mean?
@jagrit07
@jagrit07 4 жыл бұрын
Thanks, man.
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
No problem 👍
@himanshuchhikara4918
@himanshuchhikara4918 4 жыл бұрын
super amazing explanation sir
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thank you!
@anands9407
@anands9407 4 жыл бұрын
hey can u tell me when do i know when do i know it is a backtracking problem
@anands9407
@anands9407 4 жыл бұрын
btw this was the best explanation of a backtracking problem i've ever seen...keep making videos!even if noone watches i will :P
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
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!
@anands9407
@anands9407 4 жыл бұрын
@@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
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
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.
@anands9407
@anands9407 4 жыл бұрын
thanks for the help :)
@katielamber0238
@katielamber0238 2 жыл бұрын
man I love you ahahah
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you!
@aishwaryam1052
@aishwaryam1052 4 жыл бұрын
Why do we set visited[i][j]=false again?
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
To allow for that specific cell to be used in another calculation as the algorithm moves forward.
@aishwaryam1052
@aishwaryam1052 4 жыл бұрын
@@AlgosWithMichael Thank you!
@ishq_gunah_hai3179
@ishq_gunah_hai3179 4 жыл бұрын
@@AlgosWithMichael Awesome man 💗
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Thanks!
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
Anytime!
@TheElementFive
@TheElementFive 2 жыл бұрын
Returns 0 for me..
@rishavsaha5254
@rishavsaha5254 2 жыл бұрын
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
@shishirarora8808
@shishirarora8808 7 ай бұрын
O(m * n * 4^(m * n))
@manasbolt
@manasbolt 4 жыл бұрын
why are we doing visited[i][j]=false at the end of the function??
@AlgosWithMichael
@AlgosWithMichael 4 жыл бұрын
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.
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 4,7 МЛН
How many people are in the changing room? #devil #lilith #funny #shorts
00:39
МЕНЯ УКУСИЛ ПАУК #shorts
00:23
Паша Осадчий
Рет қаралды 5 МЛН