Stock span (2 testcases getting wrong)
Hello Saksham,
it looks like there is small logic error in your code .can u please explain me what is your logic?
this will help me in debugging
regrads
Aman yadav
Hello Saksham,
I have gone through your code and found some minute mistakes like
a) you need to pop from stack even if curPrice==arr[stk.top()]
for clarity here is span definition - A span of a stock’s price on a given day, i, is the maximum number of consecutive days before the (i+1)th day, for which stock’s price on these days is less than or equal to that on the ith day
b) consider case when your stack is empty in that case u were adding day only but in actual u should add day +1 because indexing starts from zero
example suppose array is 1,2,3,4
now suppose we are working out for index 3 i.e 4 in that case your stack will become empty and you will add 3 in your answer but actually u should add 4.
here is your working code with bit modifications - https://ide.codingblocks.com/s/171804
i hope it helps.
regards
Aman yadav