-
StackOverflow 文档
-
algorithm 教程
-
滑动窗口算法
-
C 语言中滑动窗口算法的实现
public class SlidingWindow
{
public static int[] MaxSlidingWindow(int[] input, int k)
{
int[] result = new int[input.Length - k + 1];
for (int i = 0; i <= input.Length - k; i++)
{
var max = input[i];
for (int j = 1; j < k; j++)
{
if (input[i + j] > max) max = input[i + j];
}
result[i] = max;
}
return result;
}
public static int[] MinSlidingWindow(int[] input, int k)
{
int[] result = new int[input.Length - k + 1];
for (int i = 0; i <= input.Length - k; i++)
{
var min = input[i];
for (int j = 1; j < k; j++)
{
if (input[i + j] < min) min = input[i + j];
}
result[i] = min;
}
return result;
}
public static int[] MainMax(int[] input, int n)
{
return MaxSlidingWindow(input, n);
}
public static int[] MainMin(int[] input, int n)
{
return MinSlidingWindow(input, n);
}
}