Find the Longest Valid Obstacle Course at Each Position - Problem

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 such that:

  • You choose any number of obstacles between 0 and i inclusive.
  • You must include the ith obstacle in the course.
  • You must put the chosen obstacles in the same order as they appear in obstacles.
  • Every obstacle (except the first) is taller than or the same height as the obstacle immediately before it.

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

Input & Output

Example 1 — Basic Sequence
$ Input: obstacles = [1,2,3,2]
Output: [1,2,3,2]
💡 Note: At index 0: longest course is [1] with length 1. At index 1: longest course is [1,2] with length 2. At index 2: longest course is [1,2,3] with length 3. At index 3: longest course is [1,2] with length 2 (can't include 3 since 3 > 2).
Example 2 — Decreasing Sequence
$ Input: obstacles = [2,2,1]
Output: [1,2,1]
💡 Note: At index 0: longest course is [2] with length 1. At index 1: longest course is [2,2] with length 2. At index 2: longest course is [1] with length 1 (can't use previous obstacles since they're taller).
Example 3 — All Same Heights
$ Input: obstacles = [3,1,5,6,4,2]
Output: [1,1,2,3,2,2]
💡 Note: At each position, we find the longest non-decreasing subsequence ending at that position. For example, at index 3 (value 6), the longest course is [1,5,6] with length 3.

Constraints

  • 1 ≤ obstacles.length ≤ 105
  • 1 ≤ obstacles[i] ≤ 107

Visualization

Tap to expand
Longest Valid Obstacle Course INPUT obstacles = [1, 2, 3, 2] Height Visualization: 1 i=0 2 i=1 3 i=2 2 i=3 Find longest non-decreasing subsequence ending at each i obstacles = [1, 2, 3, 2] ALGORITHM STEPS 1 Initialize tails[] for binary search 2 For each obstacle[i] Binary search position 3 Find upper bound Position where value fits 4 Update result ans[i] = position + 1 Processing Steps: i=0: obs=1, tails=[1], ans=1 i=1: obs=2, tails=[1,2], ans=2 i=2: obs=3, tails=[1,2,3], ans=3 i=3: obs=2, tails=[1,2,2], ans=2 Time: O(n log n) Space: O(n) FINAL RESULT Longest Course Length at Each Position 1 ans[0] 2 ans[1] 3 ans[2] 2 ans[3] Each value shows longest valid course ending at that position Output: [1, 2, 3, 2] Key Insight: This problem is a variant of Longest Increasing Subsequence (LIS) but allows equal heights. We use binary search with upper_bound to find where each obstacle fits in our tails array. The position found + 1 gives the length of longest valid course ending at current obstacle. TutorialsPoint - Find the Longest Valid Obstacle Course at Each Position | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
28.0K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen