Beautiful Row
Updated
Problem statement
People stand in a row. The height of person i is heights[i].
You may send away any of the people. Everyone who stays keeps their place in the original order. The people who stay form a beautiful row if their heights first strictly go up and then strictly go down. That is, one person is the peak: each person up to the peak is taller than the person before them, and each person after the peak is shorter than the person before them.
- Either part may be empty. A row that only goes up, a row that only goes down, and a single person are all beautiful.
- Two neighbors in a beautiful row never have the same height.
Return the largest number of people that can stay and still form a beautiful row.
Function
longestBeautifulRow(heights: int[]) → intExamples
Example 1
heights = [1,4,2,6,5,3]return = 5Keep the heights 1, 4, 6, 5, 3 and leave out 2. They go up from 1 to 4 to 6, then down from 6 to 5 to 3, so the row is beautiful and has 5 people. Keeping all six does not work: the heights go down from 4 to 2 and then up again to 6. So the answer is 5.
Example 2
heights = [5,4,4,3]return = 3Keep 5, 4, 3. The heights only go down, which is allowed: the going-up part is just the first person. Both 4s cannot be kept, because two neighbors in a beautiful row must have different heights. So the answer is 3.
Example 3
heights = [3,3,5,7]return = 3Keep 3, 5, 7, using either of the two 3s. The heights only go up, which is allowed: the going-down part is just the last person. The two 3s cannot both be kept. So the answer is 3.
Constraints
1 <= heights.length <= 10001 <= heights[i] <= 10^9