본문 바로가기

IT/코딩테스트

[Leetcode] 930. Binary Subarrays With Sum

728x90

https://leetcode.com/problems/binary-subarrays-with-sum

class Solution:
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        count = 0
        
        prifix = {0:1}
        sum = 0

        for i in nums:
            sum += i
            count += prifix.get(sum - goal, 0)
            prifix[sum] = prifix.get(sum, 0) + 1

        return count
728x90