-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sliding-window,deque): Add 3254
- Loading branch information
1 parent
2c34a9e
commit 5bfe3dd
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters