LeetCode Entry
1437. Check If All 1's Are at Least Length K Places Away
All ones k-distant
1437. Check If All 1’s Are at Least Length K Places Away easy blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1176
Problem TLDR
All ones k-distant #easy
Intuition
Count zeros in-between.
Approach
- we can write it with 1 extra variable
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 3ms
fun kLengthApart(nums: IntArray, k: Int): Boolean {
var z = k
return nums.all { n -> (n < 1 || z >= k).also { z = (1-n)*(z+1-n)}}
}
// 0ms
pub fn k_length_apart(n: Vec<i32>, k: i32) -> bool {
let mut z = k;
n.iter().all(|&n|{ let r = n < 1 || z >= k; z = (1-n)*(z+1-n); r})
}