-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3254.cpp
More file actions
23 lines (23 loc) · 723 Bytes
/
3254.cpp
File metadata and controls
23 lines (23 loc) · 723 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<int> resultsArray(vector<int>& nums, int k) {
int left = 0;
int index = 0;
int consecutive = 1;
int n = nums.size();
vector<int> res(n - k + 1, -1);
for (int right = 0; right < n; ++right) {
if (right > 0 && nums[right] - 1 == nums[right - 1]) consecutive++;
if (right - left + 1 > k) {
if (nums[left] + 1 == nums[left + 1]) consecutive--;
left++;
}
if (right - left + 1 == k) {
if (consecutive == k) res[index] = nums[right];
else res[index] = -1;
index++;
}
}
return res;
}
};