跳至主要内容

3254. Find the Power of K-Size Subarrays I

Hint

class Solution {
public:
vector<int> resultsArray(vector<int>& nums, int k)
{
if (k == 1) return nums;
vector<int> res;

int n = nums.size();

int i = 1, consecutiveSize = 1;
while (i < n)
{
if (nums[i - 1] + 1 == nums[i])
{
consecutiveSize++;
}
else
{
consecutiveSize = 1;
}
if (i + 1 >= k)
{
if (consecutiveSize >= k)
{
res.push_back(nums[i]);
}
else
{
res.push_back(-1);
}
}
i++;
}
return res;
}
};
  • T: O(n)O(n)
  • S: O(n)O(n)