Path with Maximum Gold - Leetcode 1219 - Python

  Рет қаралды 9,461

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 28
@zachandbro
@zachandbro 6 ай бұрын
Always appreciate these videos. You’re such a great explainer when it comes to these complex programming problems
@MP-ny3ep
@MP-ny3ep 6 ай бұрын
Thank you for the leetcode daily. Really appreciate it.
@nirmalgurjar8181
@nirmalgurjar8181 6 ай бұрын
As per constraints given (There are at most 25 cells containing gold) so technically solution turns out to be O(m.n) ~ O(m.n.4^25) for large number of m and n. correct me if my understanding is wrong.
@ajaysenger2
@ajaysenger2 5 ай бұрын
Solved it in one go feeling great now.
@freecourseplatformenglish2829
@freecourseplatformenglish2829 6 ай бұрын
Cool dude, I came here to verify my intution and happy that it matches with your solution.
@kiratsingh2595
@kiratsingh2595 6 ай бұрын
you should put some tags in your videos because i search path with maximum gold your video didn't show up i knew it that you uploaded the video on youtube home page but it didt come up when i searched it , then i searched it by path with maximum gold neetcode even then it didnt show up , so plz use some tags in your videos so that it can reach more people :)
@jayrathod9271
@jayrathod9271 6 ай бұрын
class Solution: def getMaximumGold(self, grid: List[List[int]]) -> int: ans ,curr= 0,0 row,col = len(grid),len(grid[0]) def dfs(i,j,curr): if i< 0 or i>=row or j=col or grid[i][j]==0: return t = grid[i][j] grid[i][j]=0 curr += t nonlocal ans dfs(i-1,j,curr) dfs(i+1,j,curr) dfs(i,j+1,curr) dfs(i,j-1,curr) ans = max(ans,curr) curr -=t grid[i][j]=t for i in range(row): for j in range(col): if grid[i][j]!=0: # curr = 0 dfs(i,j,curr) return ans
@CS_n00b
@CS_n00b 5 ай бұрын
I'm not sure what the point of having a visit local variable is since we still need to remove each visited point from the set? Why not just have a global visit set or pass visit.copy() into each dfs instance so we dont need to do visit.remove()
@yang5843
@yang5843 6 ай бұрын
Thanks for making this video
@christophervradis7285
@christophervradis7285 6 ай бұрын
Thanks Neet
@varunpalsingh3822
@varunpalsingh3822 6 ай бұрын
thank you neet :-)
@SC2Edu
@SC2Edu 6 ай бұрын
Thanks!
@chien-yuyeh9386
@chien-yuyeh9386 6 ай бұрын
Nice🎉🎉
@upsidedownChad
@upsidedownChad 6 ай бұрын
Bros cooking solving LeetCode left and right
@bobbywang9223
@bobbywang9223 6 ай бұрын
Why is this considered DFS? doesn't that only apply to graphs and trees?
@vuanhkhoa9715
@vuanhkhoa9715 6 ай бұрын
DFS is a huge topic and not only apply to graph and trees. You can try to draw a decision tree and you will see, at each cell, it will try to go as deep as it can in 4 directions to find the maximum gold it can achieve. That is DFS. And the same for BFS
@nirmalgurjar8181
@nirmalgurjar8181 6 ай бұрын
Its everywhere be it recursion, tree, graph, composite object, matrix etc. DFS, BFS are standard and can be reused in variety of problems.
@cem4960
@cem4960 6 ай бұрын
the grid in this problem is basically a graph
@jagrutitiwari2551
@jagrutitiwari2551 6 ай бұрын
If a cell is removed from visit, isn't there are a chance that it would be visited twice? Can someone explain this?
@madiyar8079
@madiyar8079 6 ай бұрын
It's explained at 6:00 this is exactly what we want, because visited cell can be visited in another backtracking flow and visiting that cell from the different path is valid.
@rahulsyt_
@rahulsyt_ 6 ай бұрын
when we exit from the call stack, we undo the changes that we made to the grid in the current call stack. And, hence it won't affect to the other calls.
@nirmalgurjar8181
@nirmalgurjar8181 6 ай бұрын
This is backtracking, we want to explore all the paths with greatest sum, once current path is evaluated, we want to reconsider same cell of other remaining path. Try drawing the solution for small inputs and figure it out.
@phanthe3471
@phanthe3471 6 ай бұрын
anyway, there are at most 25 gold cells, so the complexity should be 25*(4^25). Please correct me if i miss anything
@nirmalgurjar8181
@nirmalgurjar8181 6 ай бұрын
outer 25 will be m*n as in any case we have to run the outer loop m*n times, so m*n*4^25
@sauravsingh4497
@sauravsingh4497 6 ай бұрын
At first I thought of doing bfs/DFS from every non zero node but thought there must be a more efficient way 🥲
Maximum Matrix Sum - Leetcode 1975 - Python
16:07
NeetCodeIO
Рет қаралды 627
Rotating the Box - Leetcode 1861 - Python
15:14
NeetCodeIO
Рет қаралды 6 М.
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН
How To Choose Mac N Cheese Date Night.. 🧀
00:58
Jojo Sim
Рет қаралды 89 МЛН
1219. Path with Maximum Gold | Backtracking | Recursion
19:11
Aryan Mittal
Рет қаралды 3,1 М.
Find the Safest Path in a Grid - Leetcode 2812 - Python
26:40
NeetCodeIO
Рет қаралды 15 М.
Student Attendance Record II - Leetcode 552 - Python
27:10
NeetCodeIO
Рет қаралды 9 М.
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 14 М.
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 21 М.
5 Patterns to Master 80% of Leetcode Problems
15:37
Rahul Pandey
Рет қаралды 12 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 687 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
Binary Tree Maximum Path Sum - DFS - Leetcode 124 - Python
15:19
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН