LeetCode solutions

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

My accepted Python solution to LeetCode problem 1548, Check If All 1's Are at Least Length K Places Away, running in 11ms.

  • Difficulty: Easy
  • Python
  • Runtime 11ms
  • Memory 21MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 11ms, memory 21MB, accepted 2025-12-29.

python
class Solution:
    def kLengthApart(self, nums: List[int], k: int) -> bool:
        # Track position of last 1 seen
        # Time: O(n), Space: O(1)
        last_one = -float('inf')  # Position of last 1
        for i, num in enumerate(nums):
            if num == 1:
                if i - last_one <= k:
                    return False
                last_one = i
        return True

Source