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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean kLengthApart(int[] nums, int k) {
Integer lastOneIndex = null;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (lastOneIndex != null && i - lastOneIndex - 1 < k) {
return false;
} else {
lastOneIndex = i;
}
}
}

return true;
}
}

References

1437. Check If All 1’s Are at Least Length K Places Away