please help to rectify my code
one code is not passing
My one test case is not passing
@Bhavit_Singla
You have implemented the algorithms incorrectly.
Refer to this.
for (int i = 0; i < n; i++) {
while (!s.empty() && price[s.top()] <= price[i]) {
//Pop element from stack till new element is greater than stack elements
s.pop();
}
if(s.empty()){
//If stack is empty then current element is the greatest so far
ans[i] = i+1;
}
else {
//Current element is greater than elements after s.top()
ans[i] = i - s.top();
}
s.push(i);
}
Hi @Bhavit_Singla,
as tarun has mention, thats the correct implementation of d code.
ur present code will not work for testcases such like
5
10 20 15 25 22
for dis correct output is 1 2 1 4 1 whereas ur code will give 1 2 1 3 1.
Hope dis resolve ur 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.