I was coding on an online site. Here, there are different stages of testing your implementation ie compilation, correctness and efficiency.
I was doing a question on two pointers. Its statement is -
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). 'n' vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Your program should return an integer which corresponds to the maximum area of water that can be contained
I got correct answer verdict but on stage to check efficiency it got runtime error. Now i have two questions(second follows first one)-
1) How can it have runtime error when it was given correct answer verdict in all test cases?
2) If it is really possible, can anyone help me find the error or possible reasons that this can happen?I cant find any no matter what i think:/
Code-
int finds(vector<bool> arr){
int i=0;
while(arr[i]!=0)i++;
return i;
}
int finde(vector<bool> arr){
int i=arr.size()-1;
while(arr[i]!=0)i--;
return i;
}
struct st{
int data;
int index;
};
int Solution::maxArea(vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout http://ift.tt/2bWF24c for more details
int n=A.size(),ans=0;
vector<bool> visited(n,0);
int start=0,end=n-1;
if(n<=1)return 0;
vector<st> arr;
st temp;
for(int i=0;i<n;i++){
temp.data=A[i];
temp.index=i;
arr.push_back(temp);
}
sort(arr.begin(),arr.end(),[](st a,st b)->bool{return a.data<=b.data;});
for(int i=0;i<n-1;i++){
// cout<<start<<"\t"<<end<<endl;
ans=max(ans,arr[i].data*(arr[i].index-start));
ans=max(ans,arr[i].data*(end-arr[i].index));
visited[arr[i].index]=true;
start=finds(visited);
end=finde(visited);
//cout<<"ans is "<<ans<<endl;
}
return ans;
}
Error -
Runtime Error. Your submission stopped because of a runtime error. ex: division by zero, array index out of bounds, uncaught exception You can try testing your code with custom input and try putting debug statements in your code.
*** Error in `./solution': munmap_chunk(): invalid pointer: 0x00000000026227c0 ***
Aborted
Aucun commentaire:
Enregistrer un commentaire