Minimum Path Sum - Dynamic Programming - Leetcode 64 - Python

  Рет қаралды 41,684

NeetCode

NeetCode

Күн бұрын

Пікірлер: 35
@abhisheksunkale6672
@abhisheksunkale6672 2 жыл бұрын
i dont know why i feel more comfortable doing from top left downwards and it is accpeted with O(n) space and constant time as we use same array with edge case handling: class Solution: def minPathSum(self, grid: List[List[int]]) -> int: for row in range(len(grid)): for col in range(len(grid[0])): if row == 0 and col == 0: continue if row == 0 or col == 0: if row == 0: grid[row][col] +=grid[row][col-1] if col == 0: grid[row][col] +=grid[row-1][col] else: grid[row][col] += min(grid[row][col-1],grid[row-1][col]) return grid[-1][-1]
@parvashah7887
@parvashah7887 6 ай бұрын
Honestly, this one was simpler and easy to understand, Thanks.
@untrall6667
@untrall6667 2 жыл бұрын
I think in the video you make the [-1,-2] point to be 0, but in the code you make the [-2,-1] point to be 0. However, both are ok :)
@MrACrazyHobo
@MrACrazyHobo 3 жыл бұрын
Great explanation! It's pretty similar to the unique paths leetcode problem
@curesnow6493
@curesnow6493 2 жыл бұрын
I’ve thought this was a graph problem 😢😢
@supremoluminary
@supremoluminary 6 ай бұрын
“We don’t even have to do that brute force recursive way…” What brute force recursive way? You didn’t show anything!
@milktea2755
@milktea2755 9 ай бұрын
is it okay to solve this as a graph problem? I initially did it with the dijkstra's/min heap approach
@tedtalks2144
@tedtalks2144 2 жыл бұрын
but bro the arguments in the min() is wrong right? it should be below and right of the element.
@EddieCheng174
@EddieCheng174 2 жыл бұрын
see 8:43 it has been fixed
@harshvardhanranvirsingh9473
@harshvardhanranvirsingh9473 3 жыл бұрын
While I totally appreciate this video, but the problem must be approached first with the recursion and then the problem must be further optimized with the use of this matrix table approach. This would have helped us to clearly understand the problem
@balachandar6206
@balachandar6206 9 ай бұрын
Instead using memory modifying the same array def minPathSum(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) for row in range(m-1, -1, -1): for col in range(n-1, -1, -1): if row == m-1 and col == n-1: continue right = inf if col+1 == n else grid[row][col+1] bottom = inf if row+1 == m else grid[row+1][col] grid[row][col] = grid[row][col] + min(right, bottom) return grid[0][0]
@chineshdoshi9272
@chineshdoshi9272 2 ай бұрын
Great Explanation , but there is one bug in the code if you are specifically referring to the diagram you drawn while explaining the code. 0 position you mentioned in the code does not match the code you have written. Though it work out.
@mrditkovich2339
@mrditkovich2339 2 жыл бұрын
I love how you say "Let's write some neetcode today !"
@TheElementFive
@TheElementFive 3 жыл бұрын
Do you think it would be ok if in a machine learning engineer interview I insisted on solving this using numpy arrays? It makes shaping the grid much cleaner.
@NickDev-pd1fz
@NickDev-pd1fz 6 ай бұрын
which path needs to be chosen in case both the numbers are equal as we are comparing min of (up and left element) ?
@raylin2527
@raylin2527 2 ай бұрын
really nice!!
@JohnIdlewood
@JohnIdlewood Жыл бұрын
It took me a while to figure out that "how many subproblems do we have" - is really MxN and "how many paths are from top-left to bottom-right" is a different thing :D
@shivaprasad8919
@shivaprasad8919 Жыл бұрын
I think In for loop 4rth argument for range in unnecessary.
@BullishBuddy
@BullishBuddy 2 жыл бұрын
why can't we solve this problem with negative numbers?
@kuldeepchouhan8467
@kuldeepchouhan8467 11 ай бұрын
Great logic!! great explanation!! Well done bro!!
@kxf8608
@kxf8608 Жыл бұрын
I did a solution but suppose the walk can be FOUR-directional class Solution: def minPathSum(self, grid) -> int: R,C = len(grid), len(grid[0]) queue = [] queue.append((R-1,C-1, grid[R-1][C-1])) cache = [[float('inf')] * C for _ in range(R)] cache[R-1][C-1] = grid[R-1][C-1] while queue: for _ in range(len(queue)): i,j,cost = queue.pop(0) if i-1 >= 0 and cost + grid[i-1][j] < cache[i-1][j]: cache[i-1][j] = cost + grid[i-1][j] queue.append((i-1,j,cost + grid[i-1][j])) if i+1 < R and cost + grid[i+1][j] < cache[i+1][j]: cache[i+1][j] = cost + grid[i+1][j] queue.append((i+1,j,cost + grid[i+1][j])) if j-1 >= 0 and cost + grid[i][j-1] < cache[i][j-1]: cache[i][j-1] = cost + grid[i][j-1] queue.append((i,j-1,cost + grid[i][j-1])) if j+1 < C and cost + grid[i][j+1] < cache[i][j+1]: cache[i][j+1] = cost + grid[i][j+1] queue.append((i,j+1,cost + grid[i][j+1])) return cache[0][0]
@kxf8608
@kxf8608 Жыл бұрын
tested on [[1,1000,1,1,1],[10,10,2,1000,1],[1,1,1000,2,1],[1000,1,1,1000,1]] DP gives 1007 while BFS above gives 29
@hailei
@hailei 2 жыл бұрын
Hey NeetCode, with your drawing, I think the third line should be res[rows][cols -1] = 0, right?
@sagarpotnis1215
@sagarpotnis1215 2 жыл бұрын
it doesnt make a difference
@suchitkumar2099
@suchitkumar2099 3 жыл бұрын
grt video , i noticed you always start from reverse direction means last index instead of first
@aayushgupta6914
@aayushgupta6914 2 жыл бұрын
I don't think your code will work out for this input [ [ 1, 1, 1 ] , [ 10, 10, 2 ] , [ 10, 1, 1 ] ]. As choosing the minimum paths here can not work out well. One would have to calculate all the possible values for this case to be true.
@untrall6667
@untrall6667 2 жыл бұрын
I think it works, the answer should be 6, right?
@joydeeprony89
@joydeeprony89 Жыл бұрын
it works, 6 is answer.
@amitupadhyay6511
@amitupadhyay6511 2 жыл бұрын
I was using a bigger formula, but thanks for this small one, it is easy to implement
@vaibhavdesai16
@vaibhavdesai16 2 жыл бұрын
Thanks. I was looking for it.
@skalra8
@skalra8 2 жыл бұрын
thank you so much!
@nemesis_rc
@nemesis_rc 3 жыл бұрын
🔥🔥
@kanyestan
@kanyestan 2 жыл бұрын
Hey Neet. It's tagged as an Amazon question in the github link, just wonder where do you get the info that it's also a Google question?
@dawidprokopek7651
@dawidprokopek7651 2 жыл бұрын
it's also possible to do it in place
Unique Paths II - Leetcode 63 - Python
14:31
NeetCodeIO
Рет қаралды 16 М.
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 54 МЛН
Остановили аттракцион из-за дочки!
00:42
Victoria Portfolio
Рет қаралды 3,8 МЛН
How Strong is Tin Foil? 💪
00:26
Preston
Рет қаралды 134 МЛН
Поветкин заставил себя уважать!
01:00
МИНУС БАЛЛ
Рет қаралды 7 МЛН
Guess the Elo vs Chat!
Green Lemon Games
Рет қаралды 43
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
Stone Game - Leetcode 877 - Python
22:00
NeetCode
Рет қаралды 30 М.
Burst Baloons - Dynamic Programming - Leetcode 312 - Python
21:20
LeetCode: 64. Minimum Path Sum (Visualized)
13:29
Code With Zi
Рет қаралды 39
哈莉奎因怎么变骷髅了#小丑 #shorts
00:19
好人小丑
Рет қаралды 54 МЛН