Show problems as
Classic problem #1437EasyArray

Check If All 1's Are at Least Length K Places Away

Real-world version: Spacing Between the Marked Sleepers
Tracked in this browser only
Read Check If All 1's Are at Least Length K Places Away on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A track is logged as nums, where 1 marks a sleeper due for replacement and 0 one that is not.

Return true when every two marked sleepers have at least k unmarked sleepers between them, and false otherwise. A track with fewer than two marked sleepers always qualifies.

Examples

Example 1

Input
nums = [1, 0, 0, 0, 1, 0, 0, 1], k = 2
Output
true

The first pair of marks has three unmarked sleepers between them and the second pair has two, so both reach the required two.

Example 2

Input
nums = [1, 0, 1], k = 2
Output
false

The two marks have a single unmarked sleeper between them, which falls short of the required two.

Example 3

Input
nums = [1], k = 1
Output
true

There is only one marked sleeper, so there is no pair to be too close.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= k <= 100000
  • 0 <= nums[i] <= 1
  • k is at most nums.length

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def spacing_holds(nums: list[int], k: int) -> bool:
Java
public boolean spacingHolds(int[] nums, int k)