I am getting segmentation fault. Can you tell me what am I missing??
Histogram area(Stack)
Hello @div_yanshu07 could you please explain your complete logic here?
i think you have just complicate this question.
have you seen the editorial?
No, I haven’t seen the editorial. It’s not at all complicated I am just storing the left smaller element’s index in my left array and storing right smaller element’s index in the right array. Then in function ‘solve’ I am calculating the width of the using left and right vector and multiplying with array elements to get desired area. It’s the correct approach available in google too.
@div_yanshu07 in your left array you are pushing the index and not the elements which are smaller and also when you making the stack of pair type there is no need for it becuase even by passing the indexes you can access the elements and the index.
your solve is giving run error because when you are doing left[i] and at 0 index when you are storing -1 then there is no negative indexing in the array that why it is giving error.
@div_yanshu07 have you analysed your codes complexity?
Here for your reference i am attaching the code:
see this approach.
Yes, I already told you I am storing the index of previous smaller and next smaller element.
For eg:- 6 2 5 4 5 1 6.
Now when you are standing at 4 your previous smaller element is 2 whose index is 1 and next smaller element is 1 whose index is 5. Now width is (5-1-1=3), Therefore area a[i] i =43=12 units which is the biggest area. And as far as -1 is concerned that is not an issue because I am delaing with the value not the index.
Its a[i]= 4 X 3=12 units*
Acoording to test cases i think even after correcting your appraoch i think you will face TLE.
you are using stack as well and same logic then also you are not using the optmised approach.
i will suggest you to watch the editorial first and then if you have any doubt you can ask here:
Happy Learning!!
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.