• + 3 comments

    Try this case, it will fail for your algorithm:

    8
    6 5 8 4 7 10 9 5
    

    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.