1#ifndef MONOTONIC_STACK_H
2#define MONOTONIC_STACK_H
16 template<
typename T,
typename Comparator>
28 std::optional<T>
push(
const T &value) {
29 while (!s.empty() && comp(s.top(), value)) {
46 const T &
top()
const {
73 std::vector<T> result{};
74 result.reserve(nums.size());
76 for (
auto num: nums) {
77 auto tmp = decreasingStack.
push(num);
78 if (!tmp.has_value()) {
79 result.push_back(no_greater());
81 result.push_back(tmp.value());
100 int n =
static_cast<int>(heights.size());
101 for (
int i = 0; i <= n; i++) {
102 int h = (i == n ? 0 : heights[i]);
103 while (!s.empty() && h < heights[s.top()]) {
104 int height = heights[s.top()];
106 int width = s.empty() ? i : i - s.top() - 1;
107 maxArea = std::max(maxArea, height * width);
Definition monotonic_stack.hpp:17
std::optional< T > push(const T &value)
Definition monotonic_stack.hpp:28
bool empty() const
Definition monotonic_stack.hpp:54
const T & top() const
Definition monotonic_stack.hpp:46
Definition big_integer.hpp:14
std::vector< T > nextGreaterElement(const std::vector< T > &nums, std::function< T()> no_greater)
Definition monotonic_stack.hpp:72
static int largestRectangleInHistogram(const std::vector< int > &heights)
Definition monotonic_stack.hpp:97