数组A []仅包含'1'和'-1'
构造数组B,其中B [i]是从j开始并在i结束的最长连续子阵列的长度,其中 j < i and A[j] + .. + A[i] > 0
明显的O(n ^ 2)解决方案是:
for (int i = 0; i < A.size(); ++i) { j = i-1; sum = A[i]; B[i] = -1; //index which fills criteria not found while ( j >=0 ) { sum += A[j]; if (sum > 0) B[i] = i - j + 1; --j; } }
我正在寻找O(n)解决方案.