본문 바로가기
python-algorithm

leetcode 2348. Number of Zero-Filled Subarrays

by 무적김두칠 2023. 3. 21.

https://leetcode.com/problems/number-of-zero-filled-subarrays/description/

 

Number of Zero-Filled Subarrays - LeetCode

Can you solve this real interview question? Number of Zero-Filled Subarrays - Given an integer array nums, return the number of subarrays filled with 0. A subarray is a contiguous non-empty sequence of elements within an array.   Example 1: Input: nums =

leetcode.com

 

1
2
3
4
5
6
7
8
9
10
11
class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        answer = cnt = 0
        for i in range(len(nums)):
            if nums[i] == 0:
                cnt+=1
                answer+=cnt
            else:
                cnt = 0
        
        return answer
cs
반응형

댓글