We will receive the day stock list and day list.
Quetions is Find the nearest days whose stock price is smaller than the input day.
For example,
stockData = [1, 4, 3, 2, 1, 7, 4, 5, 3, 3]
days = [2, 3, 7, 4]
Day2: its stock price is 4 == stockData[2 -1] and the nearest day whose stock is smaller than 4 is Day3 stockData[3 - 1] == 3
Day3: its stock price is 3 == stockData[3 -1] and the nearest day whose stock is smaller than 3 is Day4 stockData[4 - 1] == 2
Day7: its stock price is 4 == stockData[7 -1] and the nearest day whose stock is smaller than 4 is Day5 or Day9 stockData[5 - 1] == 1 and stockData[9 - 1] == 3. If we find 2 days, pick the eariler day whatever their stock prices are. Thus pick Day 5.
Day4: its stock price is 2 == stockData[4 -1] and the nearest day whose stock is smaller than 2 is Day5 stockData[5 - 1] == 1
Thus final answer is [3, 4, 5, 5]
I had this question, but unfortunately 3 test cases are TLE and expect that I will not have onsite interviews.
Actually I don't know which solution will be still optimal. I just used two pointers w/ sorting.
I would appreciate your optimal solutions.
Thanks.