Skip to content
LC-1437 Easy LeetCode

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

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 58% Topics: Array
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def kLengthApart(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        prev = -k-1
        for i in xrange(len(nums)):
            if not nums[i]:
                continue
            if i-prev <= k:
                return False
            prev = i
        return True

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions