Number of Submatrices that Sum to Target - Leetcode 1074 - Python

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

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 64
@NeetCodeIO
@NeetCodeIO 10 ай бұрын
Here's the video for LeetCode 560 that I mentioned: kzbin.info/www/bejne/nHe5i6dja9iar9E
@adityansah
@adityansah 10 ай бұрын
What a lovely question for round 0 at a big tech company for Associate SDE Intern position.
@ismailkarroud3129
@ismailkarroud3129 8 ай бұрын
🤣🤣🤣🤣🤣🤣🤣
@I.II..III...IIIII.....
@I.II..III...IIIII..... 7 ай бұрын
By far the best leetcode content on youtube. What I really appreciate is that compared to others you don't pretend these solutions are easy to come up with, even for medium problems. And that's exactly how you are able to explain them so well. Thank you for the great lessons you've taught me.
@rafael84
@rafael84 10 ай бұрын
Wow, I was struggling to actually understand the formulas behind the 2D prefixSum, now I completely get it! The last part of the solution was indeed hard to grasp as well, but you once again gave us the best explanation ever! Thank you, Navdeep!
@EduarteBDO
@EduarteBDO 10 ай бұрын
I made the code a little short by increasing the sub_sum size by 1 with zeros so I don't get index out of bound and don't need the ifs. With that the first code passed with 9899ms. But I couldn't come up with a solution by myself. class Solution: def numSubmatrixSumTarget(self, matrix: list[list[int]], target: int) -> int: ROWS, COLS = len(matrix), len(matrix[0]) sub_sum = [[0] * (COLS+1) for _ in range(ROWS+1)] res = 0 for r in range(1, ROWS+1): for c in range(1, COLS+1): sub_sum[r][c] = matrix[r-1][c-1] + sub_sum[r-1][c] + sub_sum[r][c-1] - sub_sum[r-1][c-1] for r1 in range(ROWS): for r2 in range(r1, ROWS): count = defaultdict(int) # sub_sum -> count count[0] = 1 for c in range(COLS): cur_sum = sub_sum[r2+1][c+1] - sub_sum[r1][c+1] diff = cur_sum - target res += count[diff] count[cur_sum] += 1 return res
@yooos3
@yooos3 10 ай бұрын
This is one of the problems that make you feel like "you're almost there" but not exactly. Solving 506 first made this a lot easier to understand (thanks for the recommendation and video). I have a love-hate relationship with these kinda problems.
@binh-nguyen-d
@binh-nguyen-d 10 ай бұрын
Best explanation! finally i cracked this challenging problem 🎉
@kapilkhandelwal48
@kapilkhandelwal48 10 ай бұрын
In cpp it got accepted by the previous method also
@natashasrivastava7024
@natashasrivastava7024 10 ай бұрын
Thats what I was thinking.. I did previous method in java.. and accepted.. something with python I believe
@abhaykumarsingh3884
@abhaykumarsingh3884 3 ай бұрын
would you like share the solution??
@rileysikes9285
@rileysikes9285 10 ай бұрын
Truly the seventh layer of hell. Great explanation!
@rahulsiddhardh3701
@rahulsiddhardh3701 10 ай бұрын
Best explanation on the internet today
@pastori2672
@pastori2672 10 ай бұрын
easiest entry position question in 2024
@MP-ny3ep
@MP-ny3ep 10 ай бұрын
This was a tough one , thank you for explaining it so well
@estifanosbireda1892
@estifanosbireda1892 10 ай бұрын
The hint from LeetCode was really useful in this question, that helped me get to the solution. But still implementation was also not direct, had to work with index carefully.
@akhma102
@akhma102 Ай бұрын
Thank you, Neet!
@moviesins9226
@moviesins9226 10 ай бұрын
How in the world did you come with a solution for this? You are amazing. Salute!!
@NeetCodeIO
@NeetCodeIO 10 ай бұрын
I'ma keep it real with you, I wasn't able to come up with the last optimization until I saw a post that said it's similar to Leetcode 560
@XEQUTE
@XEQUTE 10 ай бұрын
keepin it real ! @@NeetCodeIO
@kgCode658
@kgCode658 10 ай бұрын
But the way you explain is amazing. Your video is as optimised as your solution. No redundancy 😅
@imatleast12
@imatleast12 10 ай бұрын
Recently I had to calculate the ∫ image to find haar like features, this is basically that. In numPy you can calculate the ∫ image by using a cumsum for the cols then the rows in just one line of code. Then to find the num of any rectangle you only need to add/subtract 4 values.
@tasneemayham974
@tasneemayham974 10 ай бұрын
Amazinggg NeetCode!! Amazing trulyy!!
@kenamia9136
@kenamia9136 10 ай бұрын
understanding leetcode 304 "Range Sum Query 2D" will make this problem easier to deal with.
@rostislav_engineer
@rostislav_engineer 3 ай бұрын
thank you
@foreverbowie2720
@foreverbowie2720 4 ай бұрын
Another challenge for my memory again!!!
@prasadm3614
@prasadm3614 10 ай бұрын
I am going to follow oncd my schedule is completed! I follow all your problems
@prasadm3614
@prasadm3614 10 ай бұрын
I love you NC
@BROOKnim
@BROOKnim 10 ай бұрын
i knew we are gonna use prefix sum. but could never come up with the fourloop to threeloop optimization , even after seeing the 1d array + map version. converting it to the 2d version wasnt intuitive to me. mayb im dumb :(
@codenrun6013
@codenrun6013 10 ай бұрын
Same
@1vader
@1vader 10 ай бұрын
Leetcode actually accepted my version of solution 1, although just barely, 9.7s time, I assume the timeout limit is 10s. Faster than 5% of Python solutions 😅
@piyushgupta7210
@piyushgupta7210 10 ай бұрын
my brain hurts now.. i was able to come up with the first solution and got TLE for 3 testcases. 37/40 passed.. however i don't think i would have thought of 2nd solution ever...
@Alexander-zt9kz
@Alexander-zt9kz 10 ай бұрын
Nobody would have been able to, the best solution I was about to come up by myself with prefix sums was O(n^3), so a bit better than O(n^4)
@BlunderArmour
@BlunderArmour 10 ай бұрын
Neato! Great explanation as always.
@yaswanthkosuru
@yaswanthkosuru 10 ай бұрын
i used to spent 2 hours to solve problem am i wasting time the key is if we have prefix sum how to get number of sub array with given target
@PrakashLight-vs1nj
@PrakashLight-vs1nj 10 ай бұрын
Same here
@varunpalsingh3822
@varunpalsingh3822 10 ай бұрын
this was hard one !!
@sankalpchordia5245
@sankalpchordia5245 10 ай бұрын
Best solution
@SalmanZaman
@SalmanZaman 10 ай бұрын
Thanks.
@StellasAdi18
@StellasAdi18 10 ай бұрын
Correct me should formula be Matrix[r2][c2]+ subsum[r1-1][c2]+ subsum[r2][c1-1] - subsum[r1-1]c1-1]?
@ayanpatel_98
@ayanpatel_98 10 ай бұрын
why do you need to new create count default dict again for every r1 and r2? why not use the same count dictionary?
@talhajaved5417
@talhajaved5417 10 ай бұрын
dankeschön
@diegobarbieri7804
@diegobarbieri7804 10 ай бұрын
thats so fireee
@superironbob
@superironbob 10 ай бұрын
What circle of hell is it that I actually got this problem in an interview?
@narendramauryaa
@narendramauryaa 10 ай бұрын
For what position?
@kgCode658
@kgCode658 10 ай бұрын
Fkkk
@kgCode658
@kgCode658 10 ай бұрын
Even after solving 1k problem I don't think anyone can come up with this soln
@CS_n00b
@CS_n00b 10 ай бұрын
What company?
@superironbob
@superironbob 10 ай бұрын
It was for a senior dev position at Google, but it was also the day they announced their first layoffs. I don't know if that's related but I'd suspect it was.
@yuvarajuv1006
@yuvarajuv1006 10 ай бұрын
How iterating through 4 loops gives us all submatrices , I am not able to get the intution . sry if i am dumb, these 2d matrix problems give me headaches
@jfelipeff
@jfelipeff 10 ай бұрын
First hehe
@ferdinanddebaecque2723
@ferdinanddebaecque2723 10 ай бұрын
Thanks for the explanation ! but I don't understand why we initialize `count[0] = 1` in the last part, what does it mean ? does someone have a simple explanation please ?
@codenrun6013
@codenrun6013 10 ай бұрын
Basically empty sub matrix is also considered, sum of that part is 0. That why we store the count as 1, which means that we have a chunk that sums upto to zero, i.e when we remove nothing from our running sum can we acheive target, if yes then ans += 1 for that case as well
@yang5843
@yang5843 10 ай бұрын
Another question to make you walk out of the interview if you get asked it.
@krishnakanth4638
@krishnakanth4638 10 ай бұрын
Have u come up the solution of this problem by ur own
@LuisEDITS_KLK
@LuisEDITS_KLK 10 ай бұрын
prob not but we came here for the explanation
@singletmat5172
@singletmat5172 10 ай бұрын
truly in the 7th layer of hell lol
@sionkim5504
@sionkim5504 10 ай бұрын
What did I just watched
@Marcox385
@Marcox385 10 ай бұрын
Another day, another brain cell Lose a few every time a hard problem comes up
@jayrathod9271
@jayrathod9271 10 ай бұрын
Second
@BROOKnim
@BROOKnim 10 ай бұрын
lessgo
@jiteshpahwa266
@jiteshpahwa266 10 ай бұрын
What was this question mannn !!!!!!!!
@rajsuriyang3427
@rajsuriyang3427 10 ай бұрын
I don't have 500IQ😂.
@nechiii-2863
@nechiii-2863 10 ай бұрын
i'll be honest ! it will take me ages to become this dude......
Cherry Pickup II - Leetcode 1463 - Python
24:00
NeetCodeIO
Рет қаралды 16 М.
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 16 М.
Do you love Blackpink?🖤🩷
00:23
Karina
Рет қаралды 21 МЛН
Yay😃 Let's make a Cute Handbag for me 👜 #diycrafts #shorts
00:33
LearnToon - Learn & Play
Рет қаралды 117 МЛН
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 138 МЛН
Continuous Subarray Sum - Leetcode 523 - Python
14:56
NeetCode
Рет қаралды 72 М.
Minimum Cost to Hire K Workers - Leetcode 857 - Python
19:01
NeetCodeIO
Рет қаралды 15 М.
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 649 М.
[CSES][Mathematics] Sum of Divisors
24:29
NeatlyStructured
Рет қаралды 8 М.
CompTIA Network+ Certification Video Course
3:46:51
PowerCert Animated Videos
Рет қаралды 8 МЛН
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Arithmetic Slices II - Leetcode 446 - Python
21:19
NeetCodeIO
Рет қаралды 17 М.
Do you love Blackpink?🖤🩷
00:23
Karina
Рет қаралды 21 МЛН