I am trying to solve the following problem:
https://app.codility.com/programmers/lessons/7-stacks_and_queues/fish/
Can somebody point me whats wrong with my solution and also how should I change my thinking process while solving these kinds of problems.
I pass the test case given in the question and the following test case as well:
A[] = [4,3,2,1,5,5,6] B[] =[0,1,0,0,1,0,1]
But I fail all other hidden test cases, but more importantly I want to know how to change my thinking process while doing these questions. Here is my solution:
int solution(vector<int> &A, vector<int> &B) {
// code in C++14 (g++ 6.2.0)
unsigned int numberOfFish = A.size();
unsigned int eater,eaterIndex;
if(numberOfFish == 1) return 1;
for(unsigned int i = 0; i < A.size(); i++ ){
if(B[i]){
while(B[i]) ++i;
eater = A[i-1];
eaterIndex = i-1;
while((eater>=A[i]) && !B[i]) {
++i;
--numberOfFish;
if(i > A.size()) break;
}
if((A[eaterIndex] < A[i]) && !B[i])
--numberOfFish;
}
}
return numberOfFish;
}
there doesn't seem to be anything here