问题描述
我必须使用Python来处理越来越多的,不连续的子数组,且其总和最大。 例如,如果我有一个数组[5、2、4、3、7、7],则必须输出[2、4、7]。 给定的输出在增加,在所有可能增加的子数组中具有最大的总和。
解决方法
看一下这段代码:
Autodesk.Viewing.Initializer(options,function onInitialized() {
viewer = new Autodesk.Viewing.GuiViewer3D(document.getElementById('MyViewerDiv'));
viewer.setTheme("light-theme");
viewer.start();
loadDocument(....)
});
上述解决方案的时间复杂度为void stat(int a[],int size,int * neg,int * zer,int * pos) {
if (size == 0) {
*neg = 0;
*zer = 0;
*pos = 0;
} else {
stat(a,size-1,neg,zer,pos);
if (a[size-1] < 0)
*neg = *neg + 1;
else if (a[size-1] == 0)
*zer = *zer + 1;
else
*pos = *pos + 1;
}
}
,程序使用的辅助空间为def elaborate(A):# Iterative function to princreasing subsequence with the maximum sum
n = len(A)
# values[i] stores the increasing subsequence having maximum sum
# that ends with A[i]
values = [[] for _ in range(n)]
values[0].append(A[0])
# sum[i] stores the maximum sum of the increasing subsequence
# that ends with A[i]
sum = [0] * n
sum[0] = A[0]
for i in range(1,n): # start from second element in the list
for j in range(i):# do for each element in sublist[0..i-1]
# find increasing subsequence with maximum sum that ends with
# A[j] where A[j] is less than the current element A[i]
if sum[i] < sum[j] and A[i] > A[j]:
values[i] = values[j].copy() # update increasing subsequence
sum[i] = sum[j] # update maximum sum
values[i].append(A[i]) # include current element in increasing subsequence
sum[i] += A[i] # add current element to maximum sum
j = 0 # j will contain index of values
for i in range(1,n):
if sum[i] > sum[j]:
j = i
# print values
print(values[j])
A = [5,2,4,3,7,7]
elaborate(A)
# OUTPUT: [2,7]