Coding Pattern:

Sliding Window

This pattern has an earnable cheat sheet.

Pattern concept

Step 1 of 21
Sliding Window Slide 1: Given a list, find the sum of all its contiguous sublists of the length 3. Consider this question:
Sliding Window Slide 2: You could iterate through the list and compute every sublist. total_sum = 6 + 9 + 12 + 15 = 42
Sliding Window Slide 3: Now what if the question were: With the previous approach, we would recompute overlapping subsets of almost 100 integers over and over again. Given a list, find the sum of all its contiguous sublists of the length 100.
Sliding Window Slide 4: total_sum = 6 When summing up the sublists, we only need to look at the difference between the current and next sublist.
Sliding Window Slide 5: 6 - 1 + 4 = 9 When summing up the sublists, we only need to look at the difference between the current and next sublist. total_sum = 6 + 9 = 15
Sliding Window Slide 6: 9 - 2 + 5 = 12 total_sum = 15 + 12 = 27 When summing up the sublists, we only need to look at the difference between the current and next sublist.
Sliding Window Slide 7: 12 - 3 + 6 = 15 total_sum = 27 + 15 = 42 When summing up the sublists, we only need to look at the difference between the current and next sublist.
Sliding Window Slide 8: Given [2, 4, 6, 3, 1, 10], find the shortest contiguous sublist that sums up to exactly 10. Sometimes instead of using a fixed sized window, we need a dynamically sized window.
Sliding Window Slide 9: a = 0 b = 1 Increment b until the sublist sums up to 10. Check the length of the sublist. Then... sum = shortest =
Sliding Window Slide 10: a = 0 b = 2 Increment b until the sublist sums up to 10. Check the length of the sublist. Then... sum = shortest =
Sliding Window Slide 11: a = 0 b = 3 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 12: a = 1 b = 3 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 13: a = 1 b = 4 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 14: a = 2 b = 4 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 15: a = 2 b = 5 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 16: a = 2 b = 6 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 17: a = 3 b = 6 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 18: a = 4 b = 6 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 19: a = 5 b = 6 sum = shortest = Increment b until the sublist sums up to 10. Check the length of the sublist. Then...
Sliding Window Slide 20: a = 6 b = 6 Since we're looking for the shortest window, we should stop iterating only when a a... sum = shortest =
Sliding Window Slide 21: You can imagine that the window mimics the expanding and contracting movement of a caterpillar.

Use ← → arrow keys

Code snippets

# Find the sum of all of the given list's contiguous sublists
# with a length of 3.
def sum_of_fixed_sublists(nums):
  if len(nums) < 3:
    return 0

  window_sum = sum(nums[:3]) # Same as nums[0] + nums[1] + nums[2]
  total_sum = window_sum

  for i in range(1, len(nums) - 2):
    # Remove old value and add new value to the window sum.
    window_sum = window_sum - nums[i - 1] + nums[i + 2]
    total_sum += window_sum

  return total_sum
# Find the shortest contiguous sublist that sums up to 10.
def shortest_of_dynamic_sublists(nums):
  a, b, window_sum, shortest = 0, 0, 0, None
  
  while a < len(nums):

    # Expand the window until the its sum is at least 10.
    while window_sum < 10 and b < len(nums):
      window_sum += nums[b]
        b += 1

    # Check if we should save the window length.
    if window_sum == 10 and (shortest is None or b - a < shortest):
      shortest = b - a
    
    # Contract the window.
    window_sum -= nums[a]
    a += 1
          
  return shortest

How to identify

Does the problem involve any of these?

Note

Most sliding window problems can be solved in O(n) time since they can be solved efficiently by updating the window instead of recomputing all elements.