- convert two nested loops into a single loop. sliding window - subarray of the original array that is of a fixes size.
- The choice of technique for solving a sliding window problem depends on the specific problem and the constraints of the problem. For example, if the sliding window is small @, then hashing may be a good choice. However, if the sliding window is large, then two pointers may be a better choice.
Fixed window problem:
In a fixed window problem, we have a predefined window size that remains constant throughout the problem-solving process. The template for solving a fixed window problem involves maintaining two pointers, low and high, that represent the indices of the current window. The process involves iterating over the array or sequence, adjusting the window as necessary, and performing computations or operations on the elements within the window.
Variable window:
The template for solving a variable window problem involves maintaining two pointers, start and end, which represent the indices of the current window.
- Expand the window: Check a condition to determine whether to expand the window. If the condition is satisfied, increment the end pointer to expand the window size.
- Process the window: Once the window size meets the desired criteria or condition, perform the required computations or operations on the elements within the window.
- Adjust the window size: If the window size exceeds the desired criteria, adjust the window by moving the start pointer. Iterate or loop until the window size matches the desired criteria, and update the window accordingly.
link