TLE despite O(n)


#1

TLE on Submission, whereas ans is correct for all inputs.
TL:O(n)
int Solution::removeElement(vector &A, int B) {
if(A.size()==0){
return 0;
}
int n=A.size();
int i=0;
int j=0;
while(i<A.size()){
if(A[i]==B){
A.erase(A.begin()+i);
}
else{
i++;
j++;
}
}
return j;
}

and the solution given is also having TL O(n).


#2

Try resize after the while instead erase inside the while. For vector erase cannot be done in O(1)… as far as I remember.