-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsol930.py
More file actions
27 lines (26 loc) · 794 Bytes
/
sol930.py
File metadata and controls
27 lines (26 loc) · 794 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing import List
# todo
class Solution930:
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
current = 0
i = j = 0
res = 0
while i < len(nums) and j < len(nums):
# find last 1 for get goal
while j < len(nums) and goal > current:
current += nums[j]
j += 1
if goal > current:
break
# find the next 1
res += 1
while j < len(nums) and current + nums[j] == goal:
res += 1
j += 1
# find the first 1 in subarray
while i < j and current - nums[i] == goal:
res += 1
i += 1
i += 1
current -= 1
return res