I submitted the same code onLeetcode and it got accepted , but here it says wrong answer
Whats wrong in it?
HISTOGRAM problem doubt
Hi… pls share ur code… only then will i be able to figure out the error…
https://online.codingblocks.com/app/player/211536/content/198532/4880/code-challenge
#include
#include
#include
#include
using namespace std;
int main() {
int n;
cin>>n;
vector<int> v,left,right;
for(int i=0;i<n;i++){
int p;
cin>>p;
v.push_back(p);
}
//nearest smallest left
stack<int> s;
for(int i=0;i<n;i++){
if(s.empty()){
left.push_back(-1);
}
else if(v[s.top()]<v[i]){
left.push_back(s.top());
}
else{
while(!s.empty() && v[s.top()]>=v[i]){
s.pop();
}
if(s.empty()){
left.push_back(-1);
}
else{
left.push_back(s.top());
}
}
s.push(i);
}
//nearest on right:
stack<int> s2;
for(int i=n-1;i>=0;i--){
if(s2.empty()){
right.push_back(n);
}
else if(v[s2.top()]<v[i]){
right.push_back(s2.top());
}
else{
while(!s2.empty() && v[s2.top()]>=v[i]){
s2.pop();
}
if(s2.empty()){
right.push_back(n);
}
else{
right.push_back(s2.top());
}
}
s2.push(i);
}
reverse(right.begin(),right.end());
int ma = 0;
for(int i=0;i<n;i++){
ma = max(ma,(right[i]-left[i]-1)*v[i]);
}
cout<<ma;
return 0;
}
do u still have any doubt???
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.