Java.Algo.SlidingWindow

simple sliding window

        int i = 0;
        int j = k;
        while (j < nums.length) {
            // do smth
            i++;
            j++;
        }

expanding sliding window

package org.example;


import java.util.Arrays;

/**
 * https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/
 */
public class Main {
    public static void main(String[] args) {

    }

    public int minimumDifference(int[] nums, int k) {
        Arrays.sort(nums);
        int minDiff = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j < nums.length; j++) {
                int slidingWindowLength = j - i + 1
                if (slidingWindowLength == k) {
                    minDiff = Math.min(minDiff, getDifference(nums, i, j));
                }
            }
        }
        return minDiff;
    }

    private int getDifference(int[] nums, int i, int j) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int k = i; k <= j; k++) {
            min = Math.min(min, nums[k]);
            max = Math.max(max, nums[k]);
        }
        return max - min;
    }
}

expanding sliding window

package org.example;


/**
 * https://leetcode.com/problems/find-the-k-beauty-of-a-number/description/
 */
public class Main {
    public static void main(String[] args) {
        divisorSubstrings(240, 2);
    }

    public static int divisorSubstrings(int num, int k) {
        String numStr = String.valueOf(num);
        int count = 0;
        for (int i = 0; i < numStr.length(); i++) {
            int j = i;
            while (j < numStr.length()) {
                if (j - i + 1 == k) {
                    int sunNum = Integer.parseInt(numStr.substring(i, j + 1));
                    if (sunNum != 0 && num % sunNum == 0) {
                        count++;
                    }
                }
                j++;
            }
        }
        return count;
    }
}

expanding sliding window example

package org.example;


import java.util.HashMap;

/**
 * https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/description/
 */
public class Main {
    public static void main(String[] args) {
        maximumLengthSubstring("acedc");
    }

    public static int maximumLengthSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            map.clear();
            int currLength = 0;
            for (int j = i; j < s.length(); j++) {
                if (map.containsKey(s.charAt(j)) && map.get(s.charAt(j)) == 2) {
                    break;
                }
                currLength = j - i + 1;
                map.put(s.charAt(j), map.getOrDefault(s.charAt(j), 0) + 1);
            }
            // sliding window jMax - i here
            max = Math.max(max, currLength);
        }
        return max;
    }
}
This entry was posted in Без рубрики. Bookmark the permalink.