The Window Sliding Technique is a technique for reducing the time complexity of algorithms using nested loops. We can avoid nested loops and obtain greater performance by traversing the array with a single loop and sliding a window over it.
A sliding window is a technique used to process sub-arrays or sub-matrices of an array or matrix sequentially. In a 2D array, a sliding window can be used to traverse all possible sub-matrices of a given size.
Here are the general steps to use a sliding window:
public static int[][] slidingWindowMax(int[][] matrix, int k) {
int numRows = matrix.length;
int numCols = matrix[0].length;
int[][] result = new int[numRows - k + 1][numCols - k + 1];
for (int i = 0; i <= numRows - k; i++) {
for (int j = 0; j <= numCols - k; j++) {
int maxVal = Integer.MIN_VALUE;
for (int x = i; x < i + k; x++) {
for (int y = j; y < j + k; y++) {
maxVal = Math.max(maxVal, matrix[x][y]);
}
}
result[i][j] = maxVal;
}
}
return result;
}
public static int[][] slidingWindowMin(int[][] matrix, int k) {
int numRows = matrix.length;
int numCols = matrix[0].length;
int[][] result = new int[numRows - k + 1][numCols - k + 1];
for (int i = 0; i <= numRows - k; i++) {
for (int j = 0; j <= numCols - k; j++) {
int minVal = Integer.MAX_VALUE;
for (int x = i; x < i + k; x++) {
for (int y = j; y < j + k; y++) {
minVal = Math.min(minVal, matrix[x][y]);
}
}
result[i][j] = minVal;
}
}
return result;
}
Here, the slidingWindowMax
function takes in a 2D array matrix
and a window size k
, and returns a new 2D array containing the maximum value in each k x k submatrix of the input array. The slidingWindowMin
function does the same thing but returns the minimum value instead.
public static int countSubstrings(String s) {
int left = 0, right = 0;
int count = 0;
int[] freq = new int[3];
int n = s.length();
while (right < n) {
freq[s.charAt(right) - 'a']++;
while (freq[0] > 0 && freq[1] > 0 && freq[2] > 0) {
count += n - right;
freq[s.charAt(left) - 'a']--;
left++;
}
right++;
}
return count;
}
To represent the beginning and finish of the window, we used two pointers to the left and right, respectively. We also use a three-dimensional array freq to keep track of the frequency of each character in the current window.
Note: also read about DSA: 2D Array
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…