Check If All 1's Are at Least Length K Places Away
Real-world version: Spacing Between the Marked SleepersThe 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.
def spacing_holds(nums: list[int], k: int) -> bool:public boolean spacingHolds(int[] nums, int k)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's