leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

check_1s_k_place_away.py (751B)


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

"""
given a binary array 'nums' and an integer 'k', return true if all 1s are at
least 'k' places away from eachother, otherwise return false.
"""


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


if __name__ == "__main__":
    obj = Solution()
    print(obj.kLengthApart(nums=[1, 0, 0, 0, 1, 0, 0, 1], k=2))
    print(obj.kLengthApart(nums=[1, 0, 0, 1, 0, 1], k=2))