Skip to content

Commit 0ab4587

Browse files
authored
Create 0930-binary-subarrays-with-sum.kt
1 parent 579c774 commit 0ab4587

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
fun numSubarraysWithSum(nums: IntArray, goal: Int): Int {
3+
4+
fun helper(x: Int): Int {
5+
if (x < 0) return 0
6+
7+
var res = 0
8+
var l = 0
9+
var cur = 0
10+
for (r in 0 until nums.size) {
11+
cur += nums[r]
12+
13+
while (cur > x)
14+
cur -= nums[l++]
15+
16+
res += (r - l + 1)
17+
}
18+
19+
return res
20+
}
21+
22+
return helper(goal) - helper(goal - 1)
23+
}
24+
}

0 commit comments

Comments
 (0)