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
'IT > 코딩테스트' 카테고리의 다른 글
[Leetcode] 525. Contiguous Array (0) | 2024.03.16 |
---|---|
[Leetcode] 238. Product of Array Except Self (0) | 2024.03.15 |
[Leetcode] 2485. Find the Pivot Integer (0) | 2024.03.13 |
[Leetcode] 791. Custom Sort String (0) | 2024.03.11 |
[Leetcode] 3074. Apple Redistribution into Boxes (0) | 2024.03.10 |