题目:给定一个长度为n,由非零整数成的数组,求连续子数组乘积为负数的个数
example:
5
5 -3 3 -1 1
7
8
ChatGPT的答案,基本正确,有个地方多+1了
n = int(input())
arr = list(map(int, input().split()))
dp1, dp2 = [0] * (n+1), [0] * (n+1)
for i in range(1, n+1):
if arr[i-1] > 0:
dp1[i] = dp1[i-1] + 1
dp2[i] = dp2[i-1]
else:
dp2[i] = dp1[i-1] + 1
dp1[i] = dp2[i-1]
print(sum(dp1))
print(sum(dp2))
标签:arr,dp2,dp1,int,sum,笔试,算法,某度,print
From: https://www.cnblogs.com/lfri/p/17189492.html