Out of Boundary Paths - Leetcode 576 - Python

  Рет қаралды 15,792

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер
@AMakYu
@AMakYu 11 ай бұрын
That 3D DP solution is wild. I was able to get the memoization solution on my own today, but I still have trouble translating that into bottom up approaches. But honestly, I've come a long ways already for being able to solve a problem like this by myself with memo. Thanks Neet!
@zaki_1337
@zaki_1337 11 ай бұрын
is it necessary to learn the iterative solutions? do interviewers ask that?
@aswinnath8580
@aswinnath8580 11 ай бұрын
they will mostly expect that one if you give only the memo one there is no guarantee you will pass the round unless if its a very hard dp problem or not a trivial one. @@zaki_1337
@AMakYu
@AMakYu 11 ай бұрын
@@zaki_1337 I think it depends on your interviewer. I think most would probably accept memo, but I had a friend who had a TikTok interview where he tried memo for a problem but it was hitting a stack limit, indicating that they wanted the bottom up approach.
@zaki_1337
@zaki_1337 11 ай бұрын
@@AMakYu oh :/
@vm1662
@vm1662 11 ай бұрын
3D DP it is! I was thinking in terms of 2D and didn't know how to memoize it. Thanks NeetCode!
@felixx2012
@felixx2012 11 ай бұрын
Thanks for doing these daily problems. Very helpful
@firehouse1395
@firehouse1395 11 ай бұрын
Your solutions are so real, nothing fancy, they are simple and easy to understand
@pastori2672
@pastori2672 11 ай бұрын
i actually got MLE on a bfs solution and a TLE on the dfs one xd
@legendary5320
@legendary5320 11 ай бұрын
One thing I was confused about the recursive brute force solution is that why are we allowed to go back to the node we just came from? Would that not consitute a path?
@EduarteBDO
@EduarteBDO 11 ай бұрын
The second solution is less efficient in LC because we are calculating the possibilities for all positions in the grid. Different from the dfs solution were we calculate for the startRow/startCol, but in a case where we wanted to calculate all of them the DP is much more faster and memory efficient.
@gmh14
@gmh14 11 ай бұрын
You mentioned a BFS solution wouldn't work but in one of my approaches I considered it and it somehow worked. Gave TLE at 22/94 but it could possibly be optimized? # BFS solution MOD = 10**9 + 7 directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] queue = [(startRow, startColumn, maxMove)] res = 0 while queue: node_i, node_j, curMoves = queue.pop(0) if curMoves > 0: curMoves -= 1 for delrow, delcol in directions: new_i, new_j = node_i + delrow, node_j + delcol if not (0
@MP-ny3ep
@MP-ny3ep 11 ай бұрын
Great explanation as always. Thank you .
@rostislav_engineer
@rostislav_engineer 10 ай бұрын
thanks, man
@krateskim4169
@krateskim4169 11 ай бұрын
Thank you so much
@priyanshuganatra
@priyanshuganatra 11 ай бұрын
Memoization solution is pretty straightforward, I dunno bout da tabu sol tho
@sankalpchordia5245
@sankalpchordia5245 11 ай бұрын
Well explained
@unknown-ut5qn
@unknown-ut5qn 11 ай бұрын
always on top
@Kaviarasu_NS
@Kaviarasu_NS 11 ай бұрын
Thanks ❤
@shashankjoshi8250
@shashankjoshi8250 11 ай бұрын
For a Brute Force Memoization I am getting TLE.
@sankalppatil2994
@sankalppatil2994 11 ай бұрын
💪💪
@EduarteBDO
@EduarteBDO 11 ай бұрын
for the if statments I mada a helper function: class Solution: def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int: ROWS,COLS = m,n MODULO = pow(10,9)+7 curGrid = [[0] * COLS for _ in range(ROWS)] prevGrid = [[0] * COLS for _ in range(ROWS)] def helper(r,c): if r < 0 or c < 0 or r == ROWS or c == COLS: return 1 return prevGrid[r][c] for _ in range(maxMove): for r in range(ROWS): for c in range(COLS): val = helper(r+1,c) val += helper(r-1,c) val += helper(r,c+1) val += helper(r,c-1) val %=MODULO curGrid[r][c] = val prevGrid, curGrid = curGrid,prevGrid return prevGrid[startRow][startColumn]
@walkastray007
@walkastray007 11 ай бұрын
Not to brag NeetCode, but I got the 15 view on this video.
@SC2Edu
@SC2Edu 11 ай бұрын
did you solve it at least? :D
@XEQUTE
@XEQUTE 11 ай бұрын
day 26 Leetcode
@shaco6630
@shaco6630 11 ай бұрын
Great explanation as usual! A suggestion, is it not a bit more readible to remove the if/else checks with something similar to this? (I added this vcrsion to neetcode github if that's ok, instead of having all the if statements) class Solution { fun findPaths(m: Int, n: Int, maxMove: Int, startRow: Int, startColumn: Int): Int { val mod = 1_000_000_007 val dirs = intArrayOf(0, 1, 0, -1, 0) val dp = Array (m) { Array (n) { LongArray (maxMove + 1) } } for (k in 1..maxMove) { for (i in 0 until m) { for (j in 0 until n) { for (dir in 0..3) { val i2 = i + dirs[dir] val j2 = j + dirs[dir + 1] if (i2 < 0 || i2 == m || j2 < 0 || j2 == n) dp[i][j][k]++ else dp[i][j][k] = (dp[i][j][k] + dp[i2][j2][k - 1]) % mod } } } } return dp[startRow][startColumn][maxMove].toInt() } }
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 17 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
My scorpion was taken away from me 😢
00:55
TyphoonFast 5
Рет қаралды 2,7 МЛН
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 14 М.
Greatest Common Divisor Traversal - Leetcode 2709 - Python
24:30
LeetCode - 58. Length of Last Word | String | Python | Java
2:57
Minimum Time to Visit a Cell In a Grid - Leetcode 2577 - Python
22:55
The Dome Paradox: A Loophole in Newton's Laws
22:59
Up and Atom
Рет қаралды 1,1 МЛН
What is mathematical thinking actually like?
9:44
Benjamin Keep, PhD, JD
Рет қаралды 24 М.
How I'd learn ML in 2025 (if I could start over)
16:24
Boris Meinardus
Рет қаралды 18 М.
Number of Dice Rolls with Target Sum - Leetcode 1155 - Python
20:20
Longest Ideal Subsequence - Leetcode 2370 - Python
28:02
NeetCodeIO
Рет қаралды 12 М.
Implement Queue using Stacks - Leetcode 232 - Python
15:23
NeetCodeIO
Рет қаралды 31 М.
It’s all not real
00:15
V.A. show / Магика
Рет қаралды 20 МЛН