Minimum Fence Painting Strokes
Updated
Problem statement
A fence consists of adjacent vertical boards, each one unit wide. Board i has height heights[i]. Return the minimum number of brush strokes needed to paint every part of the fence.
A stroke is one of the following:
- A vertical stroke paints one chosen board from bottom to top, regardless of its height.
- A horizontal stroke paints one unit-high layer across any consecutive interval of boards. Every board in the interval must reach that layer, so the brush stays in contact throughout.
For this exercise, heights are nonnegative integers and horizontal layers lie between consecutive integer heights. Repainting previously painted areas is allowed, and it still counts as a stroke. A zero-height board has no area to paint and cannot be crossed by a horizontal stroke. An empty fence requires zero strokes.
Function
minPaintStrokes(heights: int[]) → intExamples
Example 1
heights = [2,2,1,2,1]return = 3Paint one bottom horizontal layer over all five boards, then two horizontal segments on the second layer. Two strokes cannot cover this shape.
Example 2
heights = [5,5,5]return = 3Three vertical strokes beat five horizontal layers.
Example 3
heights = [0,0]return = 0Zero-height boards have no area to paint under the stated convention.
Constraints
0 <= heights.length <= 5000.0 <= heights[i] <= 10^9.- Return an integer count. A valid answer never exceeds the number of boards.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Construct a Tree from Level-Order and Inorder TraversalsAmazon · PHONE SCREEN
- Trapping Rain WaterMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Rain Kept Between WallsDocuSign · PHONE SCREENDocuSign interview questions
- Rain Water Held Between BarsAdobe · PHONE SCREENAdobe interview questions
- Predicate Expression Tree EvaluatorMongoDB · PHONE SCREENMongoDB interview questions
- Maximum Sum of HeightsVirtu Financial · OAVirtu Financial interview questions
- Alternating Jumps to the Last IndexExpedia · OAExpedia interview questions
- Tree Path GCDNewton School · PHONE SCREEN
- Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions