Find the Longest Valid Obstacle Course at Each Position
You want to find the length of the longest obstacle course at each position in the given array. To do this, you can use a dynamic programming approach with binary search.
Here’s a stepbystep guide to the solution:
 Initialize a
result
array to keep track of the lengths and atops
array to store the smallest obstacle at each length.  Iterate through the obstacles and try to find the right place for each one in the
tops
array.  Use binary search to find the right position for the current obstacle in the
tops
array.  Update the
tops
array with the current obstacle and save the length in theresult
array.
Here’s the code:


This code returns an array representing the length of the longest obstacle course at each position. It handles the constraints well, with a time complexity of O(n * log n) and a space complexity of O(n).