37,743
社区成员




def run(a):
print('a==%s'%a)
result = []
arr = []
for x in a:
if len(arr) == 0:
if x<0:
result.append(x)
else:
arr.append([x,x])
else:
if x < 0:
s = x
while s < 0:
if len(arr) == 0:
result.append(x)
break
last = arr.pop(-1)
s += last[1]
if s > 0:
last[1] = s
arr.append(last)
else:
arr.append([x,x])
result.extend([r[0] for r in arr])
print(result)
run([5, 10, 8, -8, -5])
run([-1, -5])
run([1, -1])
run([5, -10, 8, -8, -5])
a==[5, 10, 8, -8, -5]
[5, 10]
a==[-1, -5]
[-1, -5]
a==[1, -1]
[]
a==[5, -10, 8, -8, -5]
[-10, -5]def test(test_num):
test_cast = []
i=0
for n in test_num:
test_cast.append({'i':i,'ori':n,'new':n})
i+=1
while True:
flag=False
last = None
for i in test_cast:
if i['new']>0:
last=i
elif i['new']==0 or (not last and i['new']<0):
continue
else:
flag= True
if i['new']+last['new']==0:
i['new']=0
last['new']=0
elif i['new']+last['new']>0:
last['new']=i['new']+last['new']
i['new']=0
else:
i['new']=i['new']+last['new']
last['new']=0
if last['new']==0: break
if not last or not flag:
return test_cast
test_num = [5, -10, 8, -8, -5]
for i in test(test_num):
if i['new']>0 or i['new']<0:
print (i)