Longest Mountain in Array Leetcode

My code is failing 2 test cases can you please help in fixing it
problem link> https://leetcode.com/problems/longest-mountain-in-array/
my code>>
https://ide.codingblocks.com/s/597586

https://ide.codingblocks.com/s/597595 just go through my soln once… i hope it will clear all ur doubts… its very simple implementation using 2 dp arrays for storing forward and backward results and then finally computing the ans

Vaibhav I understood your code but I will really appreciate if you can tell mistake in my code bcz it is just failing 2 test cases means mind is missing that edge case and in interview this is how my mind will work so rather than just looking your approach and letting my mind skip that edge case will keep doubt in my mind
Please can u see my code and tell the error so we can fix it bcz 2 test cases only failing plz

image
okay so error was there in this part of ur code… firstly in question it is written that if no mountain exists then we should return 0… but u had initialized ur longest with -1… so just make longest = 0 initially…
secondly u have to check that ur mountain should be such that it is not just increasing or decreasing… so u have to place the condition that dp1[i]>1 and dp2[i] > 1…
just make these 2 changes and ur code will work perfectly fine…

1 Like

yeah even I just got this thing !! well thanks Vaibhav for help !!

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.