Abstract


  • Mainly used to solve Array, OOP & Linked List related problems
  • Reduce time complexity by one dimension, O(n^2) to O(n)

Fast-Slow Pointers


Leetcode Questions

Left-Right Pointers


// Java
 
public void twoPointerSort(int left, int right, int[] arr) {
    int temp;
    while (left < right) {
        temp = arr[right];
        arr[right--] = arr[left];
        arr[left++] = temp;
    }
}

Leetcode Questions

Sliding Window


3 questions to ask

  1. What should be inside the Two Pointers Window?
  2. When should we increment the left pointer?
  3. When should we increment the right pointer?

Need to keep track of the element frequency?

We can use Hash Map to keep track the element frequency.

Terminologies


Two Pointers Window

  • The elements within the left and right pointers when we are performing Sliding Window
  • The operation of expanding and shrinking the window is usually the same