1437 Check If All 1’s Are at Least Length K Places Away
Published:
Problem
Solution
Intuition
This problem requires us to count the number of zeros between 1’s in the array and return True if there are at least k zeros between each 1 in the array or False if there is a pair of 1’s that do not have k zeros in between them.
If we think about how we would solve this problem by hand, our intuition leads us to scan through the array, keep track of the position of each 1 and then subtract the position of the next 1 in order to count the distance between the two. This ends up being the most efficient way to solve this problem.
Approach
- We initialize the “initial” position of the last one as
-(k + 1)as this will ensure that we can easily keep track of the first1without needing to do anything more complicated to find our first1. - We scan through each number in the array. If the number is a
1, we check to make sure the distance between the last1and the current1is at leastk. If it is not, we can simply returnFalse. If it is at leastkaway, we update ourcurvariable which tracks the position of the last1that we have seen. - We return
Trueif we exit our linear scan without finding anyFalseconditions.
Complexity
- Time complexity: $O(n)$. We perform a linear scan of the array while performing constant time steps for each scan step.
- Space complexity: $O(1)$. We utilize constant space for our variables.
Code
```python3 [] class Solution: def kLengthApart(self, nums: List[int], k: int) -> bool: n = len(nums) cur = -(k + 1) for i in range(n): if nums[i] == 1: if i - cur <= k: return False cur = i
return True ```
