Skip to content

Commit 3ed793f

Browse files
authored
Create maximum-number-of-non-overlapping-subarrays-with-sum-equals-target.py
1 parent 1abd11d commit 3ed793f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
class Solution(object):
5+
def maxNonOverlapping(self, nums, target):
6+
"""
7+
:type nums: List[int]
8+
:type target: int
9+
:rtype: int
10+
"""
11+
lookup = {0:-1}
12+
result, accu, right = 0, 0, -1
13+
for i, num in enumerate(nums):
14+
accu += num
15+
if accu-target in lookup and lookup[accu-target] >= right:
16+
right = i
17+
result += 1 # greedy
18+
lookup[accu] = i
19+
return result

0 commit comments

Comments
 (0)