# [leetcode] 单调栈

## 原问题

### 下一个更小元素

``````vector<int> nextSmallerNumber(vector<int> &nums)
{
int n = nums.size(), idx = -1;
vector<int> res(n, -1);
stack<int> stk;
for (int i = 0; i < n; i++)
{
while (!stk.empty() && nums[stk.top()] > nums[i])
{
idx = stk.top(), stk.pop();
res[idx] = i;
}
stk.emplace(i);
}
return res;
}
``````

### 下一个更大元素

``````vector<int> nextGreaterNumber(vector<int> &&nums)
{
int n = nums.size(), idx;
vector<int> res(n, -1);
stack<int> stk;
for (int i = 0; i < n; i++)
{
while (!stk.empty() && nums[stk.top()] < nums[i])
{
idx = stk.top(), stk.pop();
res[idx] = i;
}
stk.emplace(i);
}
return res;
}
``````

## Leetcode

### 下一个更大元素 I

``````class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2)
{
unordered_map<int,int> table;
// 单调递减栈，不需要严格递减
stack<int> stk;
for (int x: nums2)
{
while (!stk.empty() && stk.top() < x)
{
table[stk.top()] = x;
stk.pop();
}
stk.emplace(x);
}
int n = nums1.size();
vector<int> res(n, -1);
for (int i=0; i<n; i++)
{
if (table.count(nums1[i]))
res[i] = table[nums1[i]];
}
return res;
}
};
``````

### 下一个更大元素 II

``````class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
if (nums.size() == 0) return {};
nums.insert(nums.end(), nums.begin(), nums.end());
int n = nums.size(), idx;
vector<int> res(n, -1);
stack<int> stk;  // 单调递减栈，不需要严格递减
for (int i=0; i<n; i++)
{
while (!stk.empty() && nums[stk.top()] < nums[i])
{
idx = stk.top(), stk.pop();
res[idx] = nums[i];
}
stk.push(i);
}
return vector<int>(res.begin(), res.begin() + n/2);
}
};
``````

``````class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
if (nums.size() == 0) return {};
int n = nums.size(), idx;
vector<int> res(n, -1);
stack<int> stk;  // 单调递减栈，不需要严格递减
for (int i=0; i<=2*n-1; i++)
{
while (!stk.empty() && nums[stk.top()] < nums[i % n])
{
idx = stk.top(), stk.pop();
res[idx] = nums[i % n];
}
stk.push(i % n);
}
return res;
}
};
``````

### 每日温度

``````class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
int n = T.size(), idx = 0;
vector<int> res(n, 0);
stack<int> stk;   // 单调递减栈
for (int i=0; i<n; i++)
{
while (!stk.empty() && T[stk.top()] < T[i])
{
idx = stk.top(), stk.pop();
res[idx] = i - idx;
}
stk.emplace(i);
}
return res;
}
};
``````

## 其他

### 两侧的更小值 I

``````#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<pair<int, int>> solve(vector<int> &nums)
{
int n = nums.size(), idx;
stack<int> stk; // 严格递增栈
vector<pair<int, int>> res(n, {-1, -1});
for (int i = 0; i < n; i++)
{
while (!stk.empty() && nums[stk.top()] > nums[i])
{
idx = stk.top(), stk.pop();
res[idx].second = i;
res[idx].first = (stk.empty() ? -1 : stk.top());
}
stk.push(i);
}
while (!stk.empty())
{
idx = stk.top(), stk.pop();
res[idx].first = (stk.empty() ? -1 : stk.top());
}
return res;
}
int main()
{
int n;
cin >> n;
vector<int> nums(n, 0);
for (int i = 0; i < n; i++) cin >> nums[i];
auto ans = solve(nums);
for (auto [x,y]: ans) printf("%d %d\n", x, y);
}
``````

### 两侧的更小值 II

``````stk: 1 3 3 (1)
^      ^
|      |
left   cur
``````

``````#include <iostream>
#include <vector>
#include <stack>
using namespace std;
vector<pair<int, int>> solve(vector<int> &nums)
{
int n = nums.size(), idx;
stack<int> stk; // 递增栈，不要求严格
vector<pair<int, int>> res(n, {-1, -1});
for (int i = 0; i < n; i++)
{
while (!stk.empty() && nums[stk.top()] > nums[i])
{
idx = stk.top(), stk.top();
vector<int> buf = {idx};
while (!stk.empty() && nums[stk.top()] == nums[idx])
buf.emplace_back(stk.top()), stk.pop();
int left = (stk.empty() ? -1 : stk.top());
for (int x : buf)
res[x].first = left, res[x].second = i;
}
stk.emplace(i);
}
while (!stk.empty())
{
idx = stk.top(), stk.top();
vector<int> buf = {idx};
while (!stk.empty() && nums[stk.top()] == nums[idx])
buf.emplace_back(stk.top()), stk.pop();
int left = (stk.empty() ? -1 : stk.top());
for (int x : buf) res[x].first = left;
}
return res;
}
int main()
{
int n;
cin >> n;
vector<int> nums(n, 0);
for (int i = 0; i < n; i++) cin >> nums[i];
auto ans = solve(nums);
for (auto [x, y] : ans) printf("%d %d\n", x, y);
}
``````

[leetcode] 单调栈

(0)
(0)