All problems
0878EasyArray

Spacing Between the Marked Sleepers

Tracked in this browser only
Write code

Trains the technique from

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

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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