<p>You want to build some obstacle courses. You are given a <strong>0-indexed</strong> integer array <code>obstacles</code> of length <code>n</code>, where <code>obstacles[i]</code> describes the height of the <code>i<sup>th</sup></code> obstacle.</p>

<p>For every index <code>i</code> between <code>0</code> and <code>n - 1</code> (<strong>inclusive</strong>), find the length of the <strong>longest obstacle course</strong> in <code>obstacles</code> such that:</p>

<ul>
	<li>You choose any number of obstacles between <code>0</code> and <code>i</code> <strong>inclusive</strong>.</li>
	<li>You must include the <code>i<sup>th</sup></code> obstacle in the course.</li>
	<li>You must put the chosen obstacles in the <strong>same order</strong> as they appear in <code>obstacles</code>.</li>
	<li>Every obstacle (except the first) is <strong>taller</strong> than or the <strong>same height</strong> as the obstacle immediately before it.</li>
</ul>

<p>Return <em>an array</em> <code>ans</code> <em>of length</em> <code>n</code>, <em>where</em> <code>ans[i]</code> <em>is the length of the <strong>longest obstacle course</strong> for index</em> <code>i</code><em> as described above</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> obstacles = [1,2,3,2]
<strong>Output:</strong> [1,2,3,3]
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
- i = 0: [<u>1</u>], [1] has length 1.
- i = 1: [<u>1</u>,<u>2</u>], [1,2] has length 2.
- i = 2: [<u>1</u>,<u>2</u>,<u>3</u>], [1,2,3] has length 3.
- i = 3: [<u>1</u>,<u>2</u>,3,<u>2</u>], [1,2,2] has length 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> obstacles = [2,2,1]
<strong>Output:</strong> [1,2,1]
<strong>Explanation: </strong>The longest valid obstacle course at each position is:
- i = 0: [<u>2</u>], [2] has length 1.
- i = 1: [<u>2</u>,<u>2</u>], [2,2] has length 2.
- i = 2: [2,2,<u>1</u>], [1] has length 1.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> obstacles = [3,1,5,6,4,2]
<strong>Output:</strong> [1,1,2,3,2,2]
<strong>Explanation:</strong> The longest valid obstacle course at each position is:
- i = 0: [<u>3</u>], [3] has length 1.
- i = 1: [3,<u>1</u>], [1] has length 1.
- i = 2: [<u>3</u>,1,<u>5</u>], [3,5] has length 2. [1,5] is also valid.
- i = 3: [<u>3</u>,1,<u>5</u>,<u>6</u>], [3,5,6] has length 3. [1,5,6] is also valid.
- i = 4: [<u>3</u>,1,5,6,<u>4</u>], [3,4] has length 2. [1,4] is also valid.
- i = 5: [3,<u>1</u>,5,6,4,<u>2</u>], [1,2] has length 2.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == obstacles.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= obstacles[i] &lt;= 10<sup>7</sup></code></li>
</ul>

class Solution:
    def longestObstacleCourseAtEachPosition(self, obstacles: List[int]) -> List[int]:
        n = len(obstacles)
        res = [1]
        lis = [float('inf')] * (n + 1)
        lis[0] = -float('inf')
        lis[1] = obstacles[0]
        for i in range(1, n):
            h = obstacles[i]

            left, right = 0, n
            while left < right - 1:
                mid = (left + right) // 2
                if lis[mid] <= h:
                    left = mid
                else:
                    right = mid - 1

            longest_prev_path_length = left
            if lis[right] <= h:
                longest_prev_path_length = right
            res.append(1 + longest_prev_path_length)
            lis[1 + longest_prev_path_length] = min(lis[1 + longest_prev_path_length], h)
        return res


















        # """
        # Use DP.
        # Use the constructed ans array to store the DP subproblems.
        # Say we have solved the problem for all j <= i. Now we want
        # to solve it for i+1.
        
        # Naive Solution:
        # Iterate over all j < i + 1. If nums[j] <= nums[i + 1], then
        # it is possible for this obstacle to be included in the course
        # ending at i+1. Take the max over ans[j]. The complexity
        # of this naive solution is O(n^2).

        # Better Solution:
        # For any i, if we want a valid course of length i+1, then there are
        # many possible different valid courses that satisfy this. And
        # each of these courses will have a different final obstacle,
        # with a possibly different obstacle height.
        # Use the lis array to store the minimum possible height of the
        # final obstacle on a course of length i + 1. This implies
        # that lis is monotonically non-decreasing.
        # It is immediate that for any j, answer[j] is the largest
        # possible course length whose minimum height of the final obstacle
        # is less than or equal to obstacles[j].
        # Due to non-decreasing property, we can binary search lis.
        # """
        # n = len(obstacles)
        # ans = [1] * n
        
        # # lis[i] stores the minimum possible height of the final obstacle
        # # on a course with length i + 1
        # lis = [float("inf")] * n
        # lis[0] = obstacles[0]

        # def search(h):
        #     if h < lis[0]:
        #         return 0
        #     if h >= lis[n - 1]:
        #         return n
            
        #     # thus lis[0] <= h <= lis[n - 1]
        #     i, j = 0, n - 1
        #     while i < j - 1:
        #         mid = (i + j) // 2
        #         if h < lis[mid]:
        #             j = mid - 1
        #         else:
        #             i = mid
        #     if lis[j] <= h:
        #         return j + 1
        #     return i + 1

        # for i in range(1, n):
        #     res = 1 + search(obstacles[i])
        #     lis[res - 1] = min(lis[res - 1], obstacles[i])
        #     ans[i] = res
        # return ans

        # """
        # Naive Solution:
        # """
        # # n = len(obstacles)
        # # ans = [1]
        # # for i in range(1, n):
        # #     res = 1
        # #     for j in range(i):
        # #         if obstacles[j] <= obstacles[i]:
        # #             res = max(res, 1 + ans[j])
        # #     ans.append(res)
        # # return ans
1655