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.
It is O(n) because you look at each element at most twice, once while pushing it on the stack (L to R) and once when popping from the stack (for scanning R to L). Once elements are popped off the stack, they're not inspected any more.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Poisonous Plants
You are viewing a single comment's thread. Return to all comments →
It is
O(n)
because you look at each element at most twice, once while pushing it on the stack (L to R) and once when popping from the stack (for scanning R to L). Once elements are popped off the stack, they're not inspected any more.