Тёмный

Count Square Submatrices with All Ones - Leetcode 1277 - Python 

NeetCodeIO
Подписаться 221 тыс.
Просмотров 10 тыс.
50% 1

Опубликовано:

 

31 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 40   
@terryli3487
@terryli3487 3 дня назад
Just got an offer from Google thanks to your videos in my prep! Could not have done it without you Neetcode!!
@shashankjoshi8250
@shashankjoshi8250 5 дней назад
Hi Neetcode, can you please post the solution of yesterday's leetcode question ? 🙏
@tawfikkoptan5781
@tawfikkoptan5781 5 дней назад
Yes it was very hard😢
@anandsahoo3711
@anandsahoo3711 5 дней назад
yess neetcode we need your help in that
@thebluedragon6385
@thebluedragon6385 5 дней назад
You need to store level and maximum height (span) wrt root for each node in a hashmap, then store maximum and second maximum span values for each level.
@shashankjoshi8250
@shashankjoshi8250 5 дней назад
@@thebluedragon6385 question is how would u reach that thought process ?
@rajsuriyang3427
@rajsuriyang3427 4 дня назад
Please man 🙏.
@DNKF
@DNKF 5 дней назад
I have yesterday LC:2458 solution for ref. First, create 3x tables, 1. Node vs level from root. 2. Node vs Height from leave. 3. Node same level cousins Max 2x heights. Func. findH is to create these 3x tables. At the end, it solve the answer by each query in queries, class Solution: def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]: node_lv = [-1] * 100001 #node level -> current lv from root where root is 0 node_h = [0] * 100001 #node height -> distance from its lowest leaf top2h = [[0] * 2 for _ in range(100001)] #Keep top 2x height at this level def findH (node, lv): #Preprocessing => create node_lv, node_h, top2h tables if not node: return 0 h = 1 + max(findH(node.left, lv + 1), findH(node.right, lv + 1)) node_lv[node.val] = lv node_h[node.val] = h if h > top2h[lv][0]: top2h[lv][1] = top2h[lv][0] top2h[lv][0] = h elif h > top2h[lv][1]: top2h[lv][1] = h return h findH(root, 0) #Start the preprocessing res = [] for q in queries: #Process each query lv = node_lv[q] h = node_h[q] maxH = top2h[lv][1] if h == top2h[lv][0] else top2h[lv][0] res.append(maxH + lv - 1) return res
@yashshukla1637
@yashshukla1637 4 дня назад
Amazing video G. Thank you so so much. Just bought your course as well
@pastori2672
@pastori2672 4 дня назад
the fact that you remember problems from years and years ago is actually impressive i cant remember yesterday's problem
@sonumukundan7988
@sonumukundan7988 2 дня назад
🥳, I've done alone without seeing solution or video, I got logic within 10-15 min, but there was lot of bug and fixing it by using print statment at many areas and finished code successfully and took 1hour and 15 min. count=0 @cache def dp(row,col): f=s=t=0 if col==len(grid[0])-1: return 0 curr=grid[row][col] if row>0 and grid[row-1][col+1]>curr: f=1+dp(row-1,col+1) else: f=0 if grid[row][col+1]>curr: s=1+dp(row,col+1) else: s=0 if rowcurr: t=1+dp(row+1,col+1) else: t=0 return max(f,s,t) for i in range(len(grid)): count=max(count,dp(i,0)) return count
@MP-ny3ep
@MP-ny3ep 5 дней назад
Beautifully explained both the methods. Thank you so much
@monismomin6759
@monismomin6759 5 дней назад
Thanks for such an intuitive and simple explanation😇
@IK-xk7ex
@IK-xk7ex 5 дней назад
Using dict instead of arrays for Bottom-Up is quite smart
@daringcalf
@daringcalf 4 дня назад
Brilliant!
@whoops9443
@whoops9443 17 часов назад
At 6:54, isn't the maximal square of the 1 to the right of the origin a 2x2 square? The origin still ends up having a 2x2 square due to the 1s below and to the diagonal having the minimum square size of 1, but I just wanted to get that clarified.
@ashishkarn9283
@ashishkarn9283 5 дней назад
The trick is that you solved a similar problem 3 years ago and still remember it. I solve a problem few months back and don't remember it at all. Could you tell me how often you revise these problems?
@bishwashkumarsah171
@bishwashkumarsah171 4 дня назад
good question i also have the same doubt i usually forget the question i did yesterday. I have never seen any revision strategy. everyone just says practice more question and that it. but some of them are classical problem and we might need to know or recall.
@SarweshParsewar
@SarweshParsewar 3 дня назад
I think its more of a experience and the effort he has put into problem solving so its intuitive for him
@harshithdesai9989
@harshithdesai9989 5 дней назад
wonderful explanation !!
@Adityaing
@Adityaing 5 дней назад
The last approach is a similar to Levenshtein distance used in minimum edit distance calculation for strings.
@pratikgehlot1973
@pratikgehlot1973 4 дня назад
If someone wants one of the previous neetcode's solution to be just updated by 1 more lines - class Solution: def countSquares(self, matrix: List[List[int]]) -> int: ROWS, COLS = len(matrix), len(matrix[0]) cache = {} def dfs(r, c): if (r, c) in cache: return cache[(r, c)] if r not in range(ROWS) or c not in range(COLS): return 0 right = dfs(r + 1, c) down = dfs(r, c + 1) dia = dfs(r + 1, c + 1) if matrix[r][c] == 1: cache[(r, c)] = 1 + min(right, down, dia) else: cache[(r, c)] = 0 return cache[(r, c)] sumVal = sum(dfs(r, c) for r in range(ROWS) for c in range(COLS)) return sumVal
@EduarteBDO
@EduarteBDO 5 дней назад
I watched until 1:38 to be able to solve the problem, I think that I jumped too fast to the solution. But as always thank you very much NeetCode!. Edit, my solution was pretty different I did not used cache neither matrix but the first intuition was the same: impl Solution { pub fn count_squares(matrix: Vec) -> i32 { let mut res = 0; let row = matrix.len(); let col = matrix[0].len(); fn count_squares( matrix: &Vec, r_start: usize, c_start: usize, r_end: usize, c_end: usize, ) -> i32 { for c in c_start..c_end { if matrix[r_end - 1][c] == 0 { return 0; } } for r in r_start..r_end { if matrix[r][c_end - 1] == 0 { return 0; } } if r_end < matrix.len() && c_end < matrix[0].len() { return 1 + count_squares(matrix, r_start, c_start, r_end + 1, c_end + 1); } return 1; } for r in 0..row { for c in 0..col { res += count_squares(&matrix, r, c, r + 1, c + 1); } } res } }
@krithinragav9811
@krithinragav9811 5 дней назад
what language
@sriramkailasam1055
@sriramkailasam1055 4 дня назад
Rust​
@anna-plink
@anna-plink 4 дня назад
Funnily enough, a brute force method has a lower runtime than the DP or the recursive one. I guess it's because the matrices are not that big in the leetcode's test cases
@vrajrana2175
@vrajrana2175 5 дней назад
I am watching your daily leetcode video after while, not because I stopped doing leetcode, cause I was able to do it without any help. Thx Neet.
@mohammedsuhail.s192
@mohammedsuhail.s192 5 дней назад
Neetcode do explain yesterday problem it's so complex because of time limit exceeded
@tair2347
@tair2347 5 дней назад
I cant quite get from the first solution how the sizes of biggest matrices that can be built from current cell correctly adds up to number of squares.
@topsy_kreds
@topsy_kreds 5 дней назад
+1
@vishvpower9330
@vishvpower9330 2 дня назад
the idea is like this suppose top left cell only results in 1x1 matrix then ouput is 1 if top left results in 2x2 matrix then it is top left 2 matrics, 1x1 and 2x2 similar;y if it is top left of 3x3 matrix then, it is top left of 3 different matrix, 1x1,2x2,and 3x3 and so on and so forth this is the logic of adding the sums. each element only adds the numberof arrays stemming from it hope this helps
@janaSdj
@janaSdj 5 дней назад
Thanks
@piyushkanadje9769
@piyushkanadje9769 5 дней назад
Great!
@empty-z4u
@empty-z4u 5 дней назад
I went out and went back in after sometime. How's the car looking? my father asked. "Me at Leetcode" I just replied. He smiled and nodded. He knew the car was washed.
@shadowalphawolf9926
@shadowalphawolf9926 5 дней назад
the first solution exceeded time limit 😆
@nofollowrobotstxt
@nofollowrobotstxt 5 дней назад
STOP THIS MADNESS HOAR
@megatron324
@megatron324 5 дней назад
forced isolation or military
@SalmanZaman
@SalmanZaman 5 дней назад
Was this a old video. How come you publish a 22 minute video just 10 minute after it was publish for the daily challenge?
@yang5843
@yang5843 5 дней назад
He has insider information
@ignaziocastrogiovanni6003
@ignaziocastrogiovanni6003 5 дней назад
It was 1 hour after the daily challenge was published and he mentioned it was almost identical to a problem he had already solved.
Далее
Harder Than It Seems? 5 Minute Timer in C++
20:10
Просмотров 196 тыс.
This Single Rule Underpins All Of Physics
32:44
Просмотров 3,4 млн
Making an Algorithm Faster
30:08
Просмотров 131 тыс.
Being Competent With Coding Is More Fun
11:13
Просмотров 104 тыс.
I Solved 100 LeetCode Problems
13:11
Просмотров 185 тыс.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 281 тыс.