Solving LeetCode 2444: Count Subarrays With Fixed Bounds Using Sliding Window πŸš€

Haocheng LinHaocheng Lin
2 min read

Introduction πŸ“Š

πŸ”’ Have you counted patterns from a list of numbers?

🌑️ Imagine you are counting the stretches of days with freezing temperatures or heat waves, that’s the problem I solved recently from LeetCode - 2444: Count Subarrays with Fixed Bounds.

πŸͺŸ In this blog, I will apply a sliding window algorithm to solve this problem with a real-world example to highlight how you can demonstrate critical thinking.

🧩 Problem Breakdown

Given:

  1. An array of numbers, nums.

  2. Two integers: minK and maxK.

Determine how many subarrays contain both:

  1. A minimum value equal to minK.

  2. A maximum value equal to maxK.

For example,

Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
Output: 2
Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].

πŸ”‘ Sliding Window Algorithm

A sliding window explores each subarray through the following steps:

  1. Iterate through nums to track the last positions of minK, maxK, and the invalid numbers.

  2. For each index i, calculate how many valid subarrays there are.

  3. Accumulate this index for every element in the list, nums.

πŸ’» Java Implementation

class Solution {
    public long countSubarrays(int[] nums, int minK, int maxK) {
        long count = 0;
        int start = -1, mini = -1, maxi = -1;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < minK || nums[i] > maxK) {
                start = i;
            }
            if (nums[i] == minK) {
                mini = i;
            }
            if (nums[i] == maxK) {
                maxi = i;
            }
            int valid = Math.max(0, Math.min(mini, maxi) - start);
            count += valid;
        }
        return count;
    }
}

⌚ Time Complexity: O(n), a single pass through the array.

πŸ“š Space Complexity: O(1), constant extra spaces.

🌍Real-life Example

πŸ“… Scenario: Tracking health vitals

πŸ«€ Imagine that you are monitoring your heart rate daily. Some days, your heart rate dips too low or spikes too high. You want to count how many continuous days your heart rate remains within the minimum acceptable rate (e.g., 90 bpm) and the maximum acceptable rate (e.g., 170 bpm). A sliding window approach can track your heart rate over the month.

πŸ”Ž Why does this approach work?

This allows examining multiple conditions while preventing backtracking to recheck the same subarrays. Smart tracking monitors each index and enhances algorithm optimisation, making it superior to its brute force counterparts.

🌟 Conclusion

Sliding window simplifies complex problems, whether you are working on your interview preparations or analysing real-world data. They can save both time and effort.

0
Subscribe to my newsletter

Read articles from Haocheng Lin directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Haocheng Lin
Haocheng Lin

πŸ’» AI Developer & Researcher @ Geologix πŸ›οΈ UCL MEng Computer Science (2019 - 23). πŸ›οΈ UCL MSc AI for Sustainable Development (2023 - 24) πŸ₯‡ Microsoft Golden Global Ambassador (2023 - 24)πŸ†