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.
Alright, the big trick to this one is trying to understand what values you can actually check from any given number, and trying to devise a method of figuring this out while you read the numbers in to manage it in roughly O(n).
First point of advice, is your going to want to use a stack. Should be obvious from where you are, but I tried a bunch of other solutions before I found the stack based one.
Second is what values can you actually check against a new input value? Given the smallest value restriction, the number of valid values it can be checked against is going to be harshly limited, and there is a way you can use that to your advantage to reduce the number of checks you need to the bare minimum. You'll want to break this down into a few cases: What does it mean when you read in a number larger then the last number? Smaller? Equal?
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
AND xor OR
You are viewing a single comment's thread. Return to all comments →
Alright, the big trick to this one is trying to understand what values you can actually check from any given number, and trying to devise a method of figuring this out while you read the numbers in to manage it in roughly O(n).
First point of advice, is your going to want to use a stack. Should be obvious from where you are, but I tried a bunch of other solutions before I found the stack based one.
Second is what values can you actually check against a new input value? Given the smallest value restriction, the number of valid values it can be checked against is going to be harshly limited, and there is a way you can use that to your advantage to reduce the number of checks you need to the bare minimum. You'll want to break this down into a few cases: What does it mean when you read in a number larger then the last number? Smaller? Equal?