We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
You're using the wrong algorithm of just scanning from Right to Left, which has apparently been mentioned by someone incorrectly in the discussion here. Apparently this broken algorithm does pass most of the test cases except the 4 pointed. :)
Instead try iterating from left to right over each plant, and calculate when each plant will die. To calculate when a plant will die, find the plant which can potentially kill it. This requires only the past information, by scanning towards the left. Using a stack here will help you with the leftwards scan.
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
Try this case, it will fail for your algorithm:
You're using the wrong algorithm of just scanning from Right to Left, which has apparently been mentioned by someone incorrectly in the discussion here. Apparently this broken algorithm does pass most of the test cases except the 4 pointed. :)
Instead try iterating from left to right over each plant, and calculate when each plant will die. To calculate when a plant will die, find the plant which can potentially kill it. This requires only the past information, by scanning towards the left. Using a stack here will help you with the leftwards scan.