Usually when you calculate the LIS/LDS of a sequence, you calculate all the partial results in one pass. That is, you calculate the LIS of the range [ 0 : i ] for 0 <= i < n.jddantes wrote:Wouldn't you need to find the LIS and LDS with each car as the starting point?
Now, your question could also be written as:
(..) find the LIS and LDS with each car as the ending point, in the reversed sequence?
Which, as you can see, is an idea that has been suggested in previous comments, and can be implemented with only one pass. No need to calculate the LIS/LDS n times.