1#ifndef SLIDING_WINDOW_H
2#define SLIDING_WINDOW_H
8#include <unordered_map>
12 template<std::
integral T>
26 while (!window.empty() && window.front().second <= currentIndex -
static_cast<int>(maxSize)) {
31 while (!window.empty() && value >= window.back().first) {
36 window.push_back(std::make_pair(value, currentIndex));
49 return window.front().first;
57 return std::min(maxSize,
static_cast<size_t>(currentIndex -
58 (window.empty() ? currentIndex : window.front().second)));
62 std::deque<std::pair<T, int>> window;
77 const int n =
static_cast<int>(nums.size());
78 std::unordered_map<int, int> m;
86 while (left <= right && m.size() > k) {
87 if (
auto it = m.find(nums[left]); it->second == 1) {
95 ans += (right - left + 1);
Definition sliding_window.hpp:13
SlidingWindowMax(size_t size)
Definition sliding_window.hpp:18
size_t size() const
Definition sliding_window.hpp:56
std::optional< T > get_max() const
Definition sliding_window.hpp:45
void update(T value)
Definition sliding_window.hpp:24
Definition big_integer.hpp:14
static int subarraysWithAtMostKDistinct(const std::vector< int > &nums, int k)
Definition sliding_window.hpp:76