Find the Longest Valid Obstacle Course at Each Position

You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle.

For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles[0..i] such that:

  • You choose some subset of obstacles between 0 and i inclusive.
  • You must include obstacles[i] as the last obstacle in the course.
  • The selected obstacles must be in non-decreasing order of height.

Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above.

Examples
Input: [1,2,3,2]
Output: [1,2,3,3]
Hints

Find the Longest Valid Obstacle Course at Each Position

You want to build some obstacle courses. You are given a 0-indexed integer array `obstacles` of length `n`, where `obstacles[i]` describes the height of the `i`th obstacle.