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.
The catch is: when we have an empty stack we are in sea level, then if we receive a 'D', we enter a Valley, and will stay inside a valley until the stack is empty again. Same thing for a mountain.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Valleys
You are viewing a single comment's thread. Return to all comments →
This is a classic stack solution. My answer in Kotlin:
The catch is: when we have an empty stack we are in sea level, then if we receive a 'D', we enter a Valley, and will stay inside a valley until the stack is empty again. Same thing for a mountain.