Timeline
Timeline
2025-11-17
init
Array
Problem:
Note that for two elements at positions i and j, the number of elements between them is j-i-1.
12345678910111213141516171819202122232425262728293031 | using std::vector;class Solution { public: bool kLengthApart(vector<int> &nums, int k) { //It is enough to check whether adjacent ones are at least k elements apart. int last = -1, i, n = nums.size(); // Find the first 1. for (i = 0; i < n; i++) { if (nums[i] == 1) { last = i; break; } } if (last == -1) { return true; } for (i = last + 1; i < n; i++) { if (nums[i] == 1) { if (i - last - 1 < k) { return false; } else { last = i; } } } return true; }}; |
