Implementation of Sliding Window Algorithm in C
suggest changepublic 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);
}
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents