Find the Longest Valid Obstacle Course at Each Position - Problem

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 0 to the current position i
  • The obstacle at position i must 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

example_1.py โ€” Basic Case
$ Input: obstacles = [1,2,3,2]
โ€บ Output: [1,2,3,3]
๐Ÿ’ก Note: At position 0: course [1] has length 1. At position 1: course [1,2] has length 2. At position 2: course [1,2,3] has length 3. At position 3: course [1,2,2] has length 3 (we can't include the 3 since 3 > 2).
example_2.py โ€” Decreasing Heights
$ Input: obstacles = [2,2,1]
โ€บ Output: [1,2,1]
๐Ÿ’ก Note: At position 0: course [2] has length 1. At position 1: course [2,2] has length 2. At position 2: course [1] has length 1 (we can only use obstacles up to position 2, and since 1 < 2, we can't extend any previous course).
example_3.py โ€” All Same Height
$ Input: obstacles = [3,1,5,6,4,2]
โ€บ Output: [1,1,2,3,2,2]
๐Ÿ’ก Note: The longest courses ending at each position: [3], [1], [1,5], [1,5,6], [1,4], [1,2]. Notice how we can build different valid courses by choosing different subsets of previous obstacles.

Constraints

  • n == obstacles.length
  • 1 โ‰ค n โ‰ค 105
  • 1 โ‰ค obstacles[i] โ‰ค 107
  • Time limit: 1 second

Visualization

Tap to expand
Obstacle Course Optimization1232Longest Course: [1,2,3]Alternative: [1,2,2]Binary Search ProcessLen 1: 1Len 2: 2Len 3: ?When processing obstacle '2':Binary search finds position 1Update: Len 2 = min(2, 2) = 2Result: Course length = 3Time: O(n log n) | Space: O(n)
Understanding the Visualization
1
Smart Placement
For each new obstacle, find the best position to place it using binary search
2
Maintain Options
Keep the smallest possible ending height for each course length
3
Optimal Choice
Either extend current best or replace to maintain better future possibilities
Key Takeaway
๐ŸŽฏ Key Insight: We only need to track the smallest ending height for each possible course length. Binary search efficiently finds the optimal placement for each new obstacle, giving us O(n log n) performance instead of O(nยฒ).
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.9K 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