Class Solution
- java.lang.Object
-
- g1901_2000.s1964_find_the_longest_valid_obstacle_course_at_each_position.Solution
-
public class Solution extends Object
1964 - Find the Longest Valid Obstacle Course at Each Position.Hard
You want to build some obstacle courses. You are given a 0-indexed integer array
obstaclesof lengthn, whereobstacles[i]describes the height of theithobstacle.For every index
ibetween0andn - 1( inclusive ), find the length of the longest obstacle course inobstaclessuch that:- You choose any number of obstacles between
0andiinclusive. - You must include the
ithobstacle 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
ansof lengthn, whereans[i]is the length of the longest obstacle course for indexias described above.Example 1:
Input: obstacles = [1,2,3,2]
Output: [1,2,3,3]
Explanation: The longest valid obstacle course at each position is:
-
i = 0: [1], [1] has length 1.
-
i = 1: [1,2], [1,2] has length 2.
-
i = 2: [1,2,3], [1,2,3] has length 3.
-
i = 3: [1,2,3,2], [1,2,2] has length 3.
Example 2:
Input: obstacles = [2,2,1]
Output: [1,2,1]
Explanation: The longest valid obstacle course at each position is:
-
i = 0: [2], [2] has length 1.
-
i = 1: [2,2], [2,2] has length 2.
-
i = 2: [2,2,1], [1] has length 1.
Example 3:
Input: obstacles = [3,1,5,6,4,2]
Output: [1,1,2,3,2,2]
Explanation: The longest valid obstacle course at each position is:
-
i = 0: [3], [3] has length 1.
-
i = 1: [3,1], [1] has length 1.
-
i = 2: [3,1,5], [3,5] has length 2. [1,5] is also valid.
-
i = 3: [3,1,5,6], [3,5,6] has length 3. [1,5,6] is also valid.
-
i = 4: [3,1,5,6,4], [3,4] has length 2. [1,4] is also valid.
-
i = 5: [3,1,5,6,4,2], [1,2] has length 2.
Constraints:
n == obstacles.length1 <= n <= 1051 <= obstacles[i] <= 107
- You choose any number of obstacles between
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]longestObstacleCourseAtEachPosition(int[] obstacles)
-