prices[i] is the price on day i. For every day, find out how many days you would have to wait until the price is strictly higher than on that day.
Return a list of the same length where element i is the number of days from day i to the nearest later day with a higher price. If there is no such day, put 0.
Comparing each day with all the following ones is O(n²). Try to do it in O(n).prices=[50, 45, 47, 60, 58] → [3, 1, 1, 0, 0]
After 50, the price first goes higher 3 days later (60). After 45 it rises the very next day (47), and after 47 too (60). Neither 60 nor 58 is ever beaten.
prices=[70, 70, 71] → [2, 1, 0]
An equal price does not count as higher: from the first 70 it takes two days to reach 71.
prices=[90, 80, 30] → [0, 0, 0]
prices=[50, 45, 47, 60, 58]
[3, 1, 1, 0, 0]
Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.