A landing page shows a bar chart of heights. The designer wants the bars to form a single peak: strictly rising up to one bar, then strictly falling after it. The peak may not be the first or last remaining bar, and at least three bars must remain.
Write fewestRemovals(heights) that returns the fewest bars to remove (keeping the others in order) so that the remaining bars form such a peak. The input always allows at least one valid peak.
Input: heights = [2,1,3,5,4,1] Output: 1
Remove the first bar: 1, 3, 5, 4, 1 rises to 5 and then falls.
Input: heights = [1,4,2] Output: 0
It already rises to 4 and falls.
3 <= heights.length <= 10001 <= heights[i] <= 10^9Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #528 Shape a Bar Chart Into a Peak. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.