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

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

NeetCodeIO

1 год назад

17,847 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@jfelipeff
@jfelipeff - 28.01.2024 07:23

First hehe

Ответить
@BROOKnim
@BROOKnim - 28.01.2024 07:23

lessgo

Ответить
@jayrathod9271
@jayrathod9271 - 28.01.2024 07:24

Second

Ответить
@prasadm3614
@prasadm3614 - 28.01.2024 07:25

I am going to follow oncd my schedule is completed! I follow all your problems

Ответить
@SalmanZaman
@SalmanZaman - 28.01.2024 07:58

Thanks.

Ответить
@yaswanthkosuru
@yaswanthkosuru - 28.01.2024 07:58

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

Ответить
@rahulsiddhardh3701
@rahulsiddhardh3701 - 28.01.2024 08:04

Best explanation on the internet today

Ответить
@BROOKnim
@BROOKnim - 28.01.2024 08:05

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 :(

Ответить
@moviesins9226
@moviesins9226 - 28.01.2024 08:16

How in the world did you come with a solution for this? You are amazing. Salute!!

Ответить
@MP-ny3ep
@MP-ny3ep - 28.01.2024 08:20

This was a tough one , thank you for explaining it so well

Ответить
@sionkim5504
@sionkim5504 - 28.01.2024 08:33

What did I just watched

Ответить
@talhajaved5417
@talhajaved5417 - 28.01.2024 09:17

dankeschön

Ответить
@kapilkhandelwal48
@kapilkhandelwal48 - 28.01.2024 09:21

In cpp it got accepted by the previous method also

Ответить
@superironbob
@superironbob - 28.01.2024 10:25

What circle of hell is it that I actually got this problem in an interview?

Ответить
@piyushgupta7210
@piyushgupta7210 - 28.01.2024 10:26

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...

Ответить
@StellasAdi18
@StellasAdi18 - 28.01.2024 11:28

Correct me should formula be

Matrix[r2][c2]+ subsum[r1-1][c2]+ subsum[r2][c1-1] - subsum[r1-1]c1-1]?

Ответить
@sankalpchordia5245
@sankalpchordia5245 - 28.01.2024 11:28

Best solution

Ответить
@binh-nguyen-d
@binh-nguyen-d - 28.01.2024 13:28

Best explanation! finally i cracked this challenging problem 🎉

Ответить
@varunpalsingh3822
@varunpalsingh3822 - 28.01.2024 14:10

this was hard one !!

Ответить
@pastori2672
@pastori2672 - 28.01.2024 14:33

easiest entry position question in 2024

Ответить
@EduarteBDO
@EduarteBDO - 28.01.2024 15:07

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

Ответить
@ferdinanddebaecque2723
@ferdinanddebaecque2723 - 28.01.2024 15:20

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 ?

Ответить
@yuvarajuv1006
@yuvarajuv1006 - 28.01.2024 16:14

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

Ответить
@diegobarbieri7804
@diegobarbieri7804 - 28.01.2024 19:22

thats so fireee

Ответить
@krishnakanth4638
@krishnakanth4638 - 28.01.2024 19:27

Have u come up the solution of this problem by ur own

Ответить
@rajsuriyang3427
@rajsuriyang3427 - 28.01.2024 19:41

I don't have 500IQ😂.

Ответить
@yooos3
@yooos3 - 28.01.2024 19:53

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.

Ответить
@BlunderArmour
@BlunderArmour - 28.01.2024 19:55

Neato! Great explanation as always.

Ответить
@imatleast12
@imatleast12 - 28.01.2024 20:09

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.

Ответить
@jiteshpahwa266
@jiteshpahwa266 - 28.01.2024 20:29

What was this question mannn !!!!!!!!

Ответить
@nechiii-2863
@nechiii-2863 - 28.01.2024 20:33

i'll be honest ! it will take me ages to become this dude......

Ответить
@1vader
@1vader - 28.01.2024 22:45

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 😅

Ответить
@rafael84
@rafael84 - 28.01.2024 22:47

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!

Ответить
@Marcox385
@Marcox385 - 28.01.2024 22:56

Another day, another brain cell
Lose a few every time a hard problem comes up

Ответить
@kenamia9136
@kenamia9136 - 28.01.2024 23:48

understanding leetcode 304 "Range Sum Query 2D" will make this problem easier to deal with.

Ответить
@rileysikes9285
@rileysikes9285 - 28.01.2024 23:51

Truly the seventh layer of hell. Great explanation!

Ответить
@estifanosbireda1892
@estifanosbireda1892 - 29.01.2024 00:05

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.

Ответить
@ayanpatel_98
@ayanpatel_98 - 29.01.2024 00:52

why do you need to new create count default dict again for every r1 and r2? why not use the same count dictionary?

Ответить
@singletmat5172
@singletmat5172 - 29.01.2024 01:06

truly in the 7th layer of hell lol

Ответить
@yang5843
@yang5843 - 29.01.2024 02:37

Another question to make you walk out of the interview if you get asked it.

Ответить
@tasneemayham974
@tasneemayham974 - 29.01.2024 04:51

Amazinggg NeetCode!! Amazing trulyy!!

Ответить
@adityansah
@adityansah - 29.01.2024 07:33

What a lovely question for round 0 at a big tech company for Associate SDE Intern position.

Ответить
@foreverbowie2720
@foreverbowie2720 - 02.07.2024 06:51

Another challenge for my memory again!!!

Ответить
@rostislav_xx
@rostislav_xx - 01.08.2024 05:54

thank you

Ответить
@akhma102
@akhma102 - 13.10.2024 03:23

Thank you, Neet!

Ответить
@gouravkamboj6937
@gouravkamboj6937 - 04.01.2025 11:22

starting 2025 with this... I promise to myself, I will crack FAANG this year.

Ответить