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
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 11ms, memory 21MB, accepted 2025-12-29.
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