Number of Zero-Filled Subarrays - Leetcode 2348 - Python

preview_player
Показать описание

Solving Leetcode 2348 - Number of Zero-Filled Subarrays, today's daily Leetcode problem on March 20th.

0:00 - Read the problem
0:50 - Drawing Explanation
7:50 - Coding Explanation

leetcode 2348

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

You can do it this way as well. (Triangular number formula)

def zeroFilledSubarray(self, nums: List[int]) -> int:
count = 0
total = 0

for i in nums:
if i == 0:
count +=1
else:
total += (count*(count+1))//2
count = 0
total += (count*(count+1))//2
return total

utkarshdewan
Автор

I didn't even wait for the coding explanation, you made it so easy Thanks a lot

asmahamdym
Автор

Watched your drawing explanation and instantly started coding up. Your teaching is fantastic!
Thank you

rhugvedbhojane
Автор

You just made it look so easy. Nice one. Keep it up.

rushabhlegion
Автор

Wow !!! This is innovative way to solve this problem !!!! Thanks !

VidyaB-ch
Автор

formulae for finding subarray is : n(n+1)/2

codsnippet
Автор

Basically the same thing, but I did it using the sum of natural numbers formula. If you find the length of the subarray, you can take the array and add the result of the formula to the answer. (length = 4, 1 + 2 + 3 + 4 == 10). The formula is (n * (n + 1)) // 2. Replace the n with the length of each subarray and you have your answer.

altusszawlowski
Автор

You could also add count to res inside the for loop, something like this:
(just personal taste)

```
def zeroFilledSubarray(self, nums: List[int]) -> int:
counter, inner = 0, 0
for i in nums:
if i == 0:
inner+=1
counter+=inner
else:
inner=0
return counter
```

karanssh
Автор

Hi,
If (l, r) is a valid subarray containing only zero, the number of valid subarrays that end at index 'r' can be calculated as '(r - l + 1)'. Thus we can solve the count for subarray (l, r) by calculating and summing the valid subarray ending at index l, l+1, ..., r. we can solve it through a sliding window technique

MohiuddinAhmedSohel
Автор

Can we do using sliding window technique?

manasamohankumar
Автор

Thanks for the video <3 <3 <3

wafer
Автор

could you please share the 75 BLIND - problems spreadsheet

SHYAMKUMAR-bcki
Автор

Why are we taking count and multiplying by 3

tino
Автор

For some reason I totally overanalyzed this. If we have an array of 0's of length n, then it can be partitioned into (n^2 + n)/2 zero-filled subarrays (gaussian sum). What I did was to iterate over the array, find the longest current run of 0's, apply the formula, then add to the result sum. Pretty stupid but maybe still interesting for the few interested in the maths behind it.

ZeonLP
Автор

Pretty sick problem. Here’s my solution
ans = t = 0
for n in nums:
if n: t = 0
else: t += 1
ans += t
return ans

vixguy
welcome to shbcf.ru