Imagine you're designing an obstacle course where participants must navigate through barriers of increasing or equal heights. You have a series of obstacles with different heights, and for each position, you want to know the longest valid course you can create.
Given a 0-indexed integer array obstacles where obstacles[i] represents the height of the i-th obstacle, your task is to find the longest obstacle course ending at each position.
Rules for a valid obstacle course:
- You can choose any obstacles from position
0to the current positioni - The obstacle at position
imust be included - Obstacles must maintain their original order from the array
- Each obstacle must be taller than or equal to the previous one
Return an array where result[i] is the length of the longest valid obstacle course ending at position i.
Example: For obstacles [1,2,3,2], the answer is [1,2,3,3] because at position 3, we can use obstacles [1,2,2] forming a course of length 3.
Input & Output
Constraints
- n == obstacles.length
- 1 โค n โค 105
- 1 โค obstacles[i] โค 107
- Time limit: 1 second