-
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);
}
}