Minimum Parking Roof Length
Updated
Problem statement
You are given distinct nonnegative integer coordinates in positions, where each coordinate contains one parked car, and an integer k.
Install one contiguous roof that covers at least k cars. A roof spanning integer coordinates x through y, inclusive, has length y - x + 1.
Return the minimum possible roof length.
Function
minimumRoofLength(positions: int[], k: int) → longExamples
Example 1
positions = [2,10,8,17]k = 3return = 9A roof from position 2 through position 10 covers cars at 2, 8, and 10 and has length 9.
Example 2
positions = [1,2,3]k = 1return = 1A length-one roof can cover any single car.
Example 3
positions = [7,3,12,4,8]k = 3return = 5Either positions 3, 4, and 7 or positions 4, 7, and 8 fit under a roof of length 5.
Constraints
1 <= positions.length <= 1000000 <= positions[i] <= 1000000000- All positions are distinct.
1 <= k <= positions.length
More American Express interview questions
- Maximum Points in a Perimeter-Bounded RectangleOA · Seen Sep 2026
- Minimum Moves to Spread Stones Across a GridOA · Seen Sep 2026
- Largest Two Non-Overlapping Buildable SquaresOA · Seen Aug 2026
- Maximum Sum of Two Sign-Flipping WindowsOA · Seen Aug 2026
- Crop Text at a Whole-Word BoundaryOA · Seen Aug 2026
- Minimum Pile Reduction StepsOA · Seen Aug 2026
- Disjoint Target-Sum TripletsPHONE SCREEN · Seen Mar 2025
Similar questions at other companies
- Count Max Num Teams 🥝Amazon · OA
- Sliding Window MedianAuxoAI · OA
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Maximum Requests in WindowSalesforce · OASalesforce interview questions
- Earliest Hour with Frequent Badge AccessIntuit · OAIntuit interview questions
- Choose One from Each Set to Minimize the RangeInMobi · OAInMobi interview questions
- Fairest Chocolate PacketsAdobe · PHONE SCREENAdobe interview questions
- Integer Compression OffsetHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
- Sliding Window Average After Trimming Both EndsGoogle · ONSITE INTERVIEWGoogle interview questions
- Detect Security Attacks from Network LogsFireworks AI · PHONE SCREEN