单调递增/递减栈

单调栈

单调栈分为单调递增栈和单调递减栈

单调递增栈:栈中元素从栈底到栈顶是递增的
单调递减栈:栈中元素从栈底到栈顶是递减的

在这里插入图片描述

应用:求解下一个大于x元素或者是小于x的元素的位置

给一个数组,返回一个大小相同的数组,返回的数组的第i个位置的值应当是,对于原数组中的第i个元素,至少往右走多少步,才能遇到一个比自己大的元素(如果之后没有比自己大的元素,或者已经是最后一个元素,则在返回数组的对应位置放上-1)
我们可以先求下一个大于x元素的值
在这里插入图片描述
在这里插入图片描述
我们发现这个栈一直都是单调的栈
我们根据代码来更好的理解一下

代码如下:

#include<iostream>
#include<stack>
using namespace std;
int num[100] = { 1,3,4,5,2,9,6 };
int res2[100];
stack<int>s;
void nextGreater(int a[], int n)
{
	for (int i = n - 1; i >= 0; i--)
	{
		while (!s.empty() && s.top() <= a[i])
		{
			s.pop();
		}
		res2[i] = s.empty() ? -1 : s.top();
		s.push(a[i]);
	}
}

int main()
{
	nextGreater(num, 7);
	for (int i = 0; i < 7; i++)
	{
		cout << res2[i] << " ";
	}
	return 0;
}

我们再来看最初的问题,我们移动几步可以遇到第一个比自己大的元素
我们来看res2数组,我们能发现什么规律呢?
在这里插入图片描述

代码如下:

#include<iostream>
#include<stack>
using namespace std;
int num[100] = { 1,3,4,5,2,9,6 };
int res[100];
struct p {
	int no;
	int data;
};
stack<p>s;
void nextGreater(int a[], int n)
{
	for (int i = n - 1; i >= 0; i--)
	{
		while (!s.empty() && s.top().data <= a[i])
		{
			s.pop();
		}
		res[i] = s.empty() ? -1 : s.top().no-i;
		p temp;
		temp.no = i;
		temp.data = num[i];
		s.push(temp);
	}
}

int main()
{
	nextGreater(num, 7);
	for (int i = 0; i < 7; i++)
	{
		cout << res[i] << " ";
	}
	return 0;
}

单调栈源码

以下是一个简单的C代码示例,用于根据单调递增递减滤除噪点: ```c #include <stdio.h> // 滤除噪点函数 void removeNoise(int arr[], int n, int isIncreasing) { // 从第二个元素开始遍历数组 for (int i = 1; i < n; i++) { // 如果当前元素和前一个元素相等,跳过 if (arr[i] == arr[i-1]) { continue; } // 如果当前元素和前一个元素不满足单调性,将其设为前一个元素 if ((isIncreasing && arr[i] < arr[i-1]) || (!isIncreasing && arr[i] > arr[i-1])) { arr[i] = arr[i-1]; } } } int main() { int arr[] = {2, 3, 4, 4, 4, 3, 6, 7, 8, 9, 8, 7, 6, 5, 4, 4, 4, 5, 6, 7}; int n = sizeof(arr) / sizeof(arr[0]); // 原数组 printf("Original array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); // 滤除噪点后的数组 removeNoise(arr, n, 1); printf("Increasing array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); removeNoise(arr, n, 0); printf("Decreasing array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; } ``` 输出结果: ``` Original array: 2 3 4 4 4 3 6 7 8 9 8 7 6 5 4 4 4 5 6 7 Increasing array: 2 3 4 4 4 4 6 7 8 9 9 9 9 9 9 9 9 9 9 9 Decreasing array: 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 6 7 7 ``` 在上面的代码示例中,`removeNoise`函数接受一个数组、数组长度和一个布尔值,以指示数组是否单调递增。它使用一个循环遍历数组,并检查每个元素是否与前一个元素相等或不满足单调性。如果是这样,它将当前元素设置为前一个元素。最后,该函数修改了原始数组,以在滤除噪点后返回一个单调递增递减的数组。 在`main`函数中,我们使用两个不同的参数调用`removeNoise`函数,分别得到单调递增递减的数组。最后,我们输出原始数组和滤除噪点后的数组。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值