[LeetCode] Adjacent Increasing Subarrays Detection I

3349. Adjacent Increasing Subarrays Detection I

Given an array nums of n integers and an integer k, determine whether there exist two adjacent subarrays of length k such that both subarrays are strictly increasing. Specifically, check if there are two subarrays starting at indices a and b (a < b), where:

  • Both subarrays nums[a..a + k - 1] and nums[b..b + k - 1] are strictly increasing.
  • The subarrays must be adjacent, meaning b = a + k.

Return true if it is possible to find two such subarrays, and false otherwise.

A subarray is a contiguous non-empty sequence of elements within an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
bool hasIncreasingSubarrays(vector<int>& nums, int k) {
deque<int> dq, dqq;
for(int i = 0; i < nums.size(); i++) {
while(dq.size() and nums[dq.back()] >= nums[i]) dq.pop_back();
while(dq.size() and dq.front() <= i - k) dq.pop_front();
while(dqq.size() and dqq.front() <= i - 2 * k) dqq.pop_front();
dq.push_back(i);
if(dq.size() == k) {
dqq.push_back(i-k+1);
if(dqq[0] == i - 2 * k + 1) return true;
}
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/11/10/PS/LeetCode/adjacent-increasing-subarrays-detection-i/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.