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.
I got it with a stack that was holding the largest previous values with their index in descending order. When I checked a new a, I could calculate how far to the left I can go before I hit a larger value, or the end of the array, by popping the values smaller than the current value off the stack. As I popped values off the stack, I can calculate the right distance for the popped value by subtracting the current index from the popped index. Once I did those calculations, I push the current value to the stack and move to the next value.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Queries with Fixed Length
You are viewing a single comment's thread. Return to all comments →
I got it with a stack that was holding the largest previous values with their index in descending order. When I checked a new a, I could calculate how far to the left I can go before I hit a larger value, or the end of the array, by popping the values smaller than the current value off the stack. As I popped values off the stack, I can calculate the right distance for the popped value by subtracting the current index from the popped index. Once I did those calculations, I push the current value to the stack and move to the next value.