Skip to content

Commit

Permalink
feat(sliding-window,deque): Add 3254
Browse files Browse the repository at this point in the history
  • Loading branch information
euchangxian committed Nov 16, 2024
1 parent 2c34a9e commit 5bfe3dd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
57 changes: 57 additions & 0 deletions C++/3254-FindThePowerOfKSizeSubarraysOne/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <cstddef>
#include <cstdlib>
#include <deque>
#include <vector>

class Solution {
public:
std::vector<int> resultsArray(std::vector<int>& nums, int k) {
// nums of length n, integer k.
// Power is defined as:
// its maximum element if all its elements are consecutive and ascending.
// -1 otherwise.
// Find power of all subarrays of size k.
// Fixed size sliding window/max deque?
//
// See element, add to deque. Should add index instead of value, so that
// we know when to remove the leftmost.
// When adding to the back, while the back element is smaller, pop
// and remove it, since we want max Deque.
// The front of the window should be the maximum of the current window.
//
// How to handle non-increasing?
// Hmmm, not sure if max-Deque is necessary. Can we exploit the consecutive
// and increasing order requirement?
std::vector<int> result;
result.reserve(nums.size() - k + 1);

std::deque<int> maxWindow;
for (int r = 0; r < nums.size(); ++r) {
// book-keep, remove from left if size is more than k
// equivalent to (r - l + 1) > k
if (!maxWindow.empty() && maxWindow.front() == r - k) {
maxWindow.pop_front();
}

// max deque, maintain invariant where the deque is monotonically
// increasing. HOWEVER, we can simply it here. IF the element in the
// window is NOT consecutive AND NOT smaller, i.e, back + 1 != nums[r]
// Then, the entire window is invalid.
if (!maxWindow.empty() && nums[maxWindow.back()] + 1 != nums[r]) {
maxWindow.clear();
}
maxWindow.push_back(r);

// check for windows of size k
if (r >= k - 1) {
if (maxWindow.size() == k) {
// the back element is the max instead.
result.push_back(nums[maxWindow.back()]);
} else {
result.push_back(-1);
}
}
}
return result;
}
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,5 @@ Now solving in C++. Like it.
| 3163 | StringCompressionThree | [![C++](assets/c++.svg)](C++/3163-StringCompressionThree/Solution.cpp) |
| 3217 | DeleteNodesFromLinkedListPresentInArray | [![C++](assets/c++.svg)](C++/3217-DeleteNodesFromLinkedListPresentInArray/Solution.cpp) |
| 3242 | DesignNeighbourSumService | [![C++](assets/c++.svg)](C++/3242-DesignNeighbourSumService/Solution.cpp) |
| 3254 | FindThePowerOfKSizeSubarraysOne | [![C++](assets/c++.svg)](C++/3254-FindThePowerOfKSizeSubarraysOne/Solution.cpp) |
| 3286 | FindASafeWalkThroughAGrid | [![C++](assets/c++.svg)](C++/3286-FindASafeWalkThroughAGrid/Solution.cpp) |

0 comments on commit 5bfe3dd

Please sign in to comment.