Loading...
Loading...
Loading...
A store records the price of a product on n days. For each day, find the first later day with a strictly higher price, and print that price. If no later day is higher, print -1.
Example
Input
4 4 5 2 25
Output
5 25 25 -1
After 4 comes 5; after 5 and after 2 comes 25; nothing is higher than 25.
Try the problem first. Open one hint at a time.
Going left to right, keep a stack of the days that are still waiting for a higher price.
For each new price, while the price on top of the stack is smaller, pop it and set its answer to the new price. Then push the new day.
Days left on the stack at the end get -1.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor