Minimum Falling Path Sum II - Leetcode 1289 - Python

  Рет қаралды 8,051

NeetCodeIO

NeetCodeIO

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/minimum...
0:00 - Read the problem
1:29 - Drawing Recursive
4:37 - Coding Recursive
9:09 - Drawing DP
14:50 - Coding DP
17:44 - Drawing Optimized DP
23:17 - Coding Optimized DP
leetcode 1289
#neetcode #leetcode #python

Пікірлер: 31
@bhuvan9956
@bhuvan9956 Ай бұрын
Love the longer videos. Basically helps me to see the entire picture. Please keep it going. Love you!
@corrogist692
@corrogist692 Ай бұрын
I used basically the same approach, but modified the grid in place top-down. Seems even less code and easier to understand!
@pratikpatel2512
@pratikpatel2512 Ай бұрын
A simple and more neat code: class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: N = len(grid) def get_min_two(r): min1, min2 = float('inf'), float('inf') min1_idx, min2_idx = -1, -1 for c in range(N): if grid[r][c]
@samridhshubham8109
@samridhshubham8109 Ай бұрын
NEAT!!
@udemezueiloabachie8626
@udemezueiloabachie8626 19 күн бұрын
This is the neatest solution in 6 lines: class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: size = len(grid) for r in range(size - 2, -1, -1): for c in range(size): grid[r][c] = grid[r][c] + min(grid[r + 1][:c] + grid[r + 1][c + 1:]) return min(grid[0])
@MP-ny3ep
@MP-ny3ep Ай бұрын
Thank you so much. Been waiting for this one.
@business_central
@business_central Ай бұрын
Dude... you are such a Hero! 🔥🔥🔥🔥
@tawfikkoptan5781
@tawfikkoptan5781 Ай бұрын
This is the first 30+ minute video om a problem I've seen you make 😂😂😂
@kebab4640
@kebab4640 Ай бұрын
Thanks sir!
@satyamjha68
@satyamjha68 Ай бұрын
Solved it!!
@meemee417
@meemee417 Ай бұрын
Did the same thing but with heap to get min 2 nums and their indices without using a helper function
@kahafb
@kahafb Ай бұрын
Funny in-place solution class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: n = len(grid) def helper(i): first, second, idx = inf, inf, None for j, val in enumerate(grid[i]): if val < first: second = first first = val idx = j elif val < second: second = val return (first, second, idx) for i in range(n-2, -1, -1): first, second, idx = helper(i+1) for j in range(n): if j != idx: grid[i][j] += first else: grid[i][j] += second return min(grid[0])
@chien-yuyeh9386
@chien-yuyeh9386 Ай бұрын
Nice🎉🎉
@advaitdongre4499
@advaitdongre4499 Ай бұрын
but at line 26, we're using the helper function right? what is the time complexity gets to bigO(n^3)? since the function is already using a for loop and then we're using it inside 2 for loops?
@dampdigits
@dampdigits Ай бұрын
Yes this is easier than the previous one. At least when it comes to being naive about time complexity.
@akialter
@akialter Ай бұрын
DP week
@pastori2672
@pastori2672 Ай бұрын
idk what it is but this is much easier then minimum height trees imo
@ik6071
@ik6071 Ай бұрын
why use tuple? and not a dict to store element-> idx for two minimum pairs?
@nikhil199029
@nikhil199029 Ай бұрын
U impemented priority queue with size 2.
@aashishbathe
@aashishbathe 22 күн бұрын
I am seeing this entire video after a long time, if anyone wants a more code optimized solution for last method, here it is :) class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: def find_smallest(row): smallest = [] for idx, num in enumerate(row): if len(smallest) < 2: smallest.append((num, idx)) elif smallest[1][0] > num: smallest.pop() smallest.append((num, idx)) smallest.sort() return smallest N = len(grid) dp = find_smallest(grid[0]) for r in range(1, N): new_dp = [] new_row = grid[r] for next_c in range(N): if next_c == dp[0][1]: new_row[next_c] += dp[1][0] else: new_row[next_c] += dp[0][0] new_dp = find_smallest(new_row) dp = new_dp return dp[0][0]
@chaitanyasharma6270
@chaitanyasharma6270 Ай бұрын
I dont get TLE with the caching solution in java
@michael._.
@michael._. Ай бұрын
in the last solution, doesn't the line: `first_row = [(val, idx) for idx, val in enumerate(grid[0])]` require $O(n)$ space complexity?
@NeetCodeIO
@NeetCodeIO Ай бұрын
Shit you're right
@michael._.
@michael._. Ай бұрын
@@NeetCodeIO i always wanted to tell you, it’s not just about solutions, but I always learned how to explain a problem comprehensively from your videos and it helped me so much, thank you :>
@AkshayAnil0-1
@AkshayAnil0-1 24 күн бұрын
Yes
@TF2Shows
@TF2Shows Ай бұрын
Can someone help? (WARNING: solution incoming, don't read if you haven't solved it yet) I'm using the O(n^2) runtime and O(1) memory solution, in which in find 1st and 2nd minimum in each row. I wrote my implementation but got stuck in test case 9: [-37, 51, -36, 34, -22], [82, 4, 30, 14, 38], [-68, -52, -92, 65, -85], [-49, -3, -77, 8, -19], [-60, -71, -21, -62, -73] The greedy path is: -37, 4, -92, -49, -73 for a total of -247. But the test case expected answer is: -268 which is the path: -37, 4, -85, -77, -73. Can someone explain why the greedy solution, which should work, doesn't work in this case? Why im the only one that fails this test case?
@kebab4640
@kebab4640 Ай бұрын
Finally lol
@leedinglin7482
@leedinglin7482 Ай бұрын
flfc
@carrotiq6879
@carrotiq6879 Ай бұрын
pls don't do TOP DOWN solution, By watching your videos I have train my brain to first think of Bottom-up solution, and it has become a habit. So continue with good old Bottom-up
@hasrat17
@hasrat17 Ай бұрын
Thanks, Can you give me referrel? I am actively looking for a Job last year I completed my degree and joined HCLTech but I didn't like working there so I resigned this month.
Freedom Trail - Leetcode 514 - Python
25:18
NeetCodeIO
Рет қаралды 13 М.
ПООСТЕРЕГИСЬ🙊🙊🙊
00:39
Chapitosiki
Рет қаралды 9 МЛН
狼来了的故事你们听过吗?#天使 #小丑 #超人不会飞
00:42
超人不会飞
Рет қаралды 61 МЛН
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 70 М.
Find the Safest Path in a Grid - Leetcode 2812 - Python
26:40
NeetCodeIO
Рет қаралды 10 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 130 М.
Longest Palindrome - Leetcode 409 - Python
12:20
NeetCodeIO
Рет қаралды 1,1 М.
Greatest Common Divisor Traversal - Leetcode 2709 - Python
24:30
Subarrays with K Different Integers - Leetcode 992 - Python
17:31
Sum of All Subsets XOR Total - Leetcode 1863 - Python
18:26
NeetCodeIO
Рет қаралды 8 М.
Minimum Height Trees - Leetcode 310 - Python
23:30
NeetCodeIO
Рет қаралды 13 М.
Should you grind LeetCode? ft. NeetCode | Backend Banter 051
56:22
Backend Banter
Рет қаралды 31 М.
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 11 М.
ПООСТЕРЕГИСЬ🙊🙊🙊
00:39
Chapitosiki
Рет қаралды 9 МЛН