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

  Рет қаралды 14,914

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 66
@pastori2672
@pastori2672 8 ай бұрын
easiest entry position question in 2024
@adityansah
@adityansah 8 ай бұрын
What a lovely question for round 0 at a big tech company for Associate SDE Intern position.
@ismailkarroud3129
@ismailkarroud3129 6 ай бұрын
🤣🤣🤣🤣🤣🤣🤣
@NeetCodeIO
@NeetCodeIO 8 ай бұрын
Here's the video for LeetCode 560 that I mentioned: kzbin.info/www/bejne/nHe5i6dja9iar9E
@kapilkhandelwal48
@kapilkhandelwal48 8 ай бұрын
In cpp it got accepted by the previous method also
@natashasrivastava7024
@natashasrivastava7024 8 ай бұрын
Thats what I was thinking.. I did previous method in java.. and accepted.. something with python I believe
@abhaykumarsingh3884
@abhaykumarsingh3884 Ай бұрын
would you like share the solution??
@rileysikes9285
@rileysikes9285 8 ай бұрын
Truly the seventh layer of hell. Great explanation!
@yuvarajuv1006
@yuvarajuv1006 8 ай бұрын
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
@yaswanthkosuru
@yaswanthkosuru 8 ай бұрын
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 8 ай бұрын
Same here
@sionkim5504
@sionkim5504 8 ай бұрын
What did I just watched
@BROOKnim
@BROOKnim 8 ай бұрын
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 8 ай бұрын
Same
@yooos3
@yooos3 8 ай бұрын
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 8 ай бұрын
Best explanation! finally i cracked this challenging problem 🎉
@rafael84
@rafael84 8 ай бұрын
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 8 ай бұрын
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
@Marcox385
@Marcox385 8 ай бұрын
Another day, another brain cell Lose a few every time a hard problem comes up
@piyushgupta7210
@piyushgupta7210 8 ай бұрын
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 8 ай бұрын
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)
@yang5843
@yang5843 8 ай бұрын
Another question to make you walk out of the interview if you get asked it.
@foreverbowie2720
@foreverbowie2720 3 ай бұрын
Another challenge for my memory again!!!
@tasneemayham974
@tasneemayham974 8 ай бұрын
Amazinggg NeetCode!! Amazing trulyy!!
@mnaresh3382
@mnaresh3382 8 ай бұрын
Just 5 min in the video, already getting goosebumps, you never fail to impress me, 👍
@venkateshv626
@venkateshv626 8 ай бұрын
The first 17 mins is just the rudimentary brute force solution explanation. It’s after that will you feel the pain in ass.
@mnaresh3382
@mnaresh3382 8 ай бұрын
@@venkateshv626 just when I thought its easier than expected, had a whole new twist waiting patiently.
@superironbob
@superironbob 8 ай бұрын
What circle of hell is it that I actually got this problem in an interview?
@narendramauryaa
@narendramauryaa 8 ай бұрын
For what position?
@kgCode658
@kgCode658 8 ай бұрын
Fkkk
@kgCode658
@kgCode658 8 ай бұрын
Even after solving 1k problem I don't think anyone can come up with this soln
@CS_n00b
@CS_n00b 8 ай бұрын
What company?
@superironbob
@superironbob 8 ай бұрын
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.
@jiteshpahwa266
@jiteshpahwa266 8 ай бұрын
What was this question mannn !!!!!!!!
@jfelipeff
@jfelipeff 8 ай бұрын
First hehe
@1vader
@1vader 8 ай бұрын
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 😅
@imatleast12
@imatleast12 8 ай бұрын
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.
@kenamia9136
@kenamia9136 8 ай бұрын
understanding leetcode 304 "Range Sum Query 2D" will make this problem easier to deal with.
@StellasAdi18
@StellasAdi18 8 ай бұрын
Correct me should formula be Matrix[r2][c2]+ subsum[r1-1][c2]+ subsum[r2][c1-1] - subsum[r1-1]c1-1]?
@singletmat5172
@singletmat5172 8 ай бұрын
truly in the 7th layer of hell lol
@I.II..III...IIIII.....
@I.II..III...IIIII..... 6 ай бұрын
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.
@estifanosbireda1892
@estifanosbireda1892 8 ай бұрын
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.
@varunpalsingh3822
@varunpalsingh3822 8 ай бұрын
this was hard one !!
@rostislav_engineer
@rostislav_engineer 2 ай бұрын
thank you
@ayanpatel_98
@ayanpatel_98 8 ай бұрын
why do you need to new create count default dict again for every r1 and r2? why not use the same count dictionary?
@MP-ny3ep
@MP-ny3ep 8 ай бұрын
This was a tough one , thank you for explaining it so well
@rahulsiddhardh3701
@rahulsiddhardh3701 8 ай бұрын
Best explanation on the internet today
@nechiii-2863
@nechiii-2863 8 ай бұрын
i'll be honest ! it will take me ages to become this dude......
@moviesins9226
@moviesins9226 8 ай бұрын
How in the world did you come with a solution for this? You are amazing. Salute!!
@NeetCodeIO
@NeetCodeIO 8 ай бұрын
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 8 ай бұрын
keepin it real ! @@NeetCodeIO
@kgCode658
@kgCode658 8 ай бұрын
But the way you explain is amazing. Your video is as optimised as your solution. No redundancy 😅
@ferdinanddebaecque2723
@ferdinanddebaecque2723 8 ай бұрын
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 8 ай бұрын
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
@rajsuriyang3427
@rajsuriyang3427 8 ай бұрын
I don't have 500IQ😂.
@BlunderArmour
@BlunderArmour 8 ай бұрын
Neato! Great explanation as always.
@krishnakanth4638
@krishnakanth4638 8 ай бұрын
Have u come up the solution of this problem by ur own
@LuisEDITS_KLK
@LuisEDITS_KLK 8 ай бұрын
prob not but we came here for the explanation
@diegobarbieri7804
@diegobarbieri7804 8 ай бұрын
thats so fireee
@sankalpchordia5245
@sankalpchordia5245 8 ай бұрын
Best solution
@jayrathod9271
@jayrathod9271 8 ай бұрын
Second
@BROOKnim
@BROOKnim 8 ай бұрын
lessgo
@SalmanZaman
@SalmanZaman 8 ай бұрын
Thanks.
@talhajaved5417
@talhajaved5417 8 ай бұрын
dankeschön
@prasadm3614
@prasadm3614 8 ай бұрын
I am going to follow oncd my schedule is completed! I follow all your problems
@prasadm3614
@prasadm3614 8 ай бұрын
I love you NC
Cherry Pickup II - Leetcode 1463 - Python
24:00
NeetCodeIO
Рет қаралды 16 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 90 М.
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 12 МЛН
小天使和小丑太会演了!#小丑#天使#家庭#搞笑
00:25
家庭搞笑日记
Рет қаралды 30 МЛН
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,2 МЛН
Spongebob ate Patrick 😱 #meme #spongebob #gmod
00:15
Mr. LoLo
Рет қаралды 19 МЛН
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 38 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 236 М.
Microservices Gone Wrong at DoorDash
17:22
NeetCodeIO
Рет қаралды 129 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 396 М.
Continuous Subarray Sum - Leetcode 523 - Python
14:56
NeetCode
Рет қаралды 68 М.
K Inverse Pairs Array - Leetcode 629 - Python
29:44
NeetCodeIO
Рет қаралды 16 М.
Everyone Should Use WebAssembly
3:34:09
Tsoding Daily
Рет қаралды 46 М.
Why Isn't Functional Programming the Norm? - Richard Feldman
46:09
Inside Out 2: ENVY & DISGUST STOLE JOY's DRINKS!!
00:32
AnythingAlexia
Рет қаралды 12 МЛН