Minimum Path Sum - Dynamic Programming - Leetcode 64 - Python

  Рет қаралды 44,764

NeetCode

NeetCode

Күн бұрын

Пікірлер: 36
@abhisheksunkale6672
@abhisheksunkale6672 3 жыл бұрын
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 8 ай бұрын
Honestly, this one was simpler and easy to understand, Thanks.
@MrACrazyHobo
@MrACrazyHobo 3 жыл бұрын
Great explanation! It's pretty similar to the unique paths leetcode problem
@mrditkovich2339
@mrditkovich2339 2 жыл бұрын
I love how you say "Let's write some neetcode today !"
@balachandar6206
@balachandar6206 Жыл бұрын
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]
@techandmore12
@techandmore12 15 күн бұрын
Alternatively in for loop: if c + 1 < COLS and r + 1 < ROWS: grid[r][c] += min(grid[r][c + 1], grid[r + 1][c]) elif c + 1 < COLS: grid[r][c] += grid[r][c + 1] elif r + 1 < ROWS: grid[r][c] += grid[r + 1][c]
@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
@milktea2755
@milktea2755 11 ай бұрын
is it okay to solve this as a graph problem? I initially did it with the dijkstra's/min heap approach
@untrall6667
@untrall6667 3 жыл бұрын
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 :)
@seenumadhavan4451
@seenumadhavan4451 2 ай бұрын
class Solution: def minPathSum(self, grid): lrow, lcol = len(grid), len(grid[0]) for i in range(lrow - 1, -1, -1): for j in range(lcol - 1, -1, -1): if i == lrow - 1 and j == lcol - 1: continue a, b = float("inf"), float("inf") if 0
@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
@NickDev-pd1fz
@NickDev-pd1fz 8 ай бұрын
which path needs to be chosen in case both the numbers are equal as we are comparing min of (up and left element) ?
@chineshdoshi9272
@chineshdoshi9272 5 ай бұрын
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.
@kuldeepchouhan8467
@kuldeepchouhan8467 Жыл бұрын
Great logic!! great explanation!! Well done bro!!
@amitupadhyay6511
@amitupadhyay6511 3 жыл бұрын
I was using a bigger formula, but thanks for this small one, it is easy to implement
@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.
@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
@vaibhavdesai16
@vaibhavdesai16 2 жыл бұрын
Thanks. I was looking for it.
@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
@curesnow6493
@curesnow6493 2 жыл бұрын
I’ve thought this was a graph problem 😢😢
@BullishBuddy
@BullishBuddy 2 жыл бұрын
why can't we solve this problem with negative numbers?
@shivaprasad8919
@shivaprasad8919 Жыл бұрын
I think In for loop 4rth argument for range in unnecessary.
@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?
@kxf8608
@kxf8608 2 жыл бұрын
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 2 жыл бұрын
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
@supremoluminary
@supremoluminary 8 ай бұрын
“We don’t even have to do that brute force recursive way…” What brute force recursive way? You didn’t show anything!
@raylin2527
@raylin2527 5 ай бұрын
really nice!!
@aayushgupta6914
@aayushgupta6914 3 жыл бұрын
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 3 жыл бұрын
I think it works, the answer should be 6, right?
@joydeeprony89
@joydeeprony89 2 жыл бұрын
it works, 6 is answer.
@dawidprokopek7651
@dawidprokopek7651 2 жыл бұрын
it's also possible to do it in place
@nemesis_rc
@nemesis_rc 3 жыл бұрын
🔥🔥
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 169 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 123 МЛН
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 29 МЛН
Minimum Path Sum
8:52
Kevin Naughton Jr.
Рет қаралды 57 М.
Edit Distance - Dynamic Programming - Leetcode 72 - Python
21:00
Unique Paths II - Leetcode 63 - Python
14:31
NeetCodeIO
Рет қаралды 19 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 631 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 856 М.
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Simplify Path - Stack - Leetcode 71 - Python
12:47
NeetCode
Рет қаралды 63 М.