-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathContinuous Subarray Sum.py
More file actions
42 lines (33 loc) · 1.22 KB
/
Continuous Subarray Sum.py
File metadata and controls
42 lines (33 loc) · 1.22 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the
index of the first number and the index of the last number. (If their are duplicate answer, return anyone)
Example
Give [-3, 1, 3, -3, 4], return [1,4].
"""
__author__ = 'Daniel'
from collections import namedtuple
Sum = namedtuple("Sum", "sum i j") # data structure to store the sum and the starting and ending index.
class Solution:
def continuousSubarraySum(self, A):
"""
Break at 0
:param A: an integer array
:return: A list of integers includes the index of the first number and the index of the last number
"""
if len(A) < 1:
return [-1, -1]
ret = Sum(A[0], 0, 0)
cur = 0 # current sum
s = 0
for e, v in enumerate(A):
cur += v
if ret.sum < cur:
ret = Sum(cur, s, e)
if cur < 0:
s = e+1
cur = 0
return [ret.i, ret.j]
if __name__ == "__main__":
assert Solution().continuousSubarraySum(
[-101, -33, -44, -55, -67, -78, -101, -33, -44, -55, -67, -78, -100, -200, -1000, -22, -100, -200, -1000, -22]
) == [15, 15]