Advertisement
smj007

Untitled

Feb 8th, 2024
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. class Solution:
  2.     def subarraySum(self, nums: List[int], k: int) -> int:
  3.  
  4.         from collections import defaultdict
  5.         prefix_sum = defaultdict(int)
  6.         running_sum = 0
  7.         count = 0
  8.  
  9.         for num in nums:
  10.             running_sum = running_sum + num
  11.  
  12.             if running_sum == k:
  13.                 count = count + 1
  14.            
  15.             if running_sum - k in prefix_sum:
  16.                 count = count + prefix_sum[running_sum - k]
  17.  
  18.             prefix_sum[running_sum] += 1
  19.  
  20.         return count
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement