https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away

Easy problem, we just need to iterate over numbers and keep distance dist to the last occurence of 1.

  1. If we meet 0, we need to increase distance by 1.
  2. If we meet 1 and current distance is more or equal than k, than everything is OK and we need to update dist = 0.
  3. If we meet 1 and current distance is less than k, than there will be two 1 placed less than k indexes away, so we can immedietly return False.
  4. In the end we return True if we reached this place.

Complexity: time complexity is O(n) to pass over our data once, space comlexity is O(1).

class Solution:
    def kLengthApart(self, nums, k):
        dist = k
        
        for num in nums:
            if num == 0: dist += 1
            elif num == 1 and dist >= k: dist = 0
            else: return False
                
        return True

If you like the solution, you can upvote it on leetcode discussion section: Problem 1437