Leetcode Note: Go - Check if All 1s Are at Least Length K Places Away
Check If All 1’s Are at Least Length K Places Away - LeetCode
https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/
- Go 言語で取り組んだメモ
回答
Go solution faster than 100% with explanation - Check If All 1’s Are at Least Length K Places Away - LeetCode
https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/solutions/1033207/go-solution-faster-than-100-with-explanation/
func kLengthApart(nums []int, k int) bool {
old := -1
for i, num := range nums {
if num == 1 {
if old != -1 && nums[old] == 1 && i - old - 1 < k {
return false
}
old = i
}
}
return true
}