// you can also use includes, for example:
// #include <algorithm>
int solution(vector<int> &A) {
// write your code in C++98
//...limit result
int MAX_LIMIT = 1000000000;
//...keep record of prefix zero numbers to reach the time complexity O(n)
int totalPassCnt = 0;
int prefixZeroCnt = 0;
for(int i = 0; i < A.size(); ++i)
{
if(A[i] == 0) prefixZeroCnt++;
else totalPassCnt += prefixZeroCnt;
if(totalPassCnt > MAX_LIMIT) return -1;
}
//...return result
return totalPassCnt;
}[codility]Passing-cars
最新推荐文章于 2023-10-09 11:09:49 发布
本文介绍了一种使用C++实现的算法,该算法用于计算整数数组中零元素的前缀数量,并通过遍历数组来计算总通过次数,确保时间复杂度为O(n)。如果总通过次数超过预设的最大限制,则返回-1。

516

被折叠的 条评论
为什么被折叠?



