Ctrl K

Parking Lot Spacing

Easy
A row of parking spots is given as the array spots: 1 means a car is parked there, 0 means the spot is free. The rules require at least gap free spots between any two neighbouring cars. Return True if the whole row follows the rule, and False otherwise.
Example 1
spots=[1, 0, 0, 1, 0, 0, 0, 1], gap=2 → True

There are 2 free spots between the first and second car and 3 between the second and third. Both gaps are at least 2.

Example 2
spots=[0, 1, 0, 1, 0, 0], gap=2 → False

Only one free spot separates the two cars, and at least 2 are required.

Example 3
spots=[0, 0, 1, 0], gap=4 → True

There is only one car, so there is nothing to keep a distance from.

Constraints
  • 1 ≤ len(spots) ≤ 10⁵
  • 0 ≤ gap ≤ len(spots)
  • spots[i] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
spots=[1, 0, 0, 1, 0, 0, 0, 1], gap=2
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.