This is the second part of a three-part series on Algorithm Templates: Two Pointers. In the first part, we delved into the first two types of the two pointers technique. Check it out if you missed it.
In this guide (Part 2), we will focus on another two common usages:
For each type, we will learn about it through two steps:
Another common usage is putting pointers on the left-most side and right-most side. One pointer starts from the beginning, while the other pointer starts from the end. They move toward each other until they meet in the middle.
1# left & right boundary: left-> <-right 2def left_right_boundary(self, seq): 3 left, right = 0, len(seq) - 1 4 while left < right: 5 # left index moves when satisfy the condition 6 if self.left_condition(left): 7 left += 1 8 # right index move when satisfy the condition 9 if self.right_condition(right): 10 right -= 1 11 # process logic before or after pointers movement 12 self.process_logic(left, right)
To better understand the theory of left and right boundary, you can refer to the following example chart.
In general, the algorithm can be used in two ways based on this technology:
The most classic and famous example is binary search. We find the target by shrinking the lower and upper bounds continuously. We cut off the half of the interval in each iteration. The following example is a version with both lower and upper bounds included.
1# [lo, hi] version, modify to [lo, hi) version as you need 2def binary_search(arr, target): 3 lo, hi = 0, len(arr) - 1 4 while lo <= hi: 5 mid = (lo + hi) // 2 6 # find the target, change to your comparison condition as you need 7 if arr[mid] == target: 8 break 9 elif arr[mid] < target: 10 lo = mid + 1 11 else: 12 hi = mid - 1
Let's take a look at a practical example with two pointers and binary search technique.
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
1#  https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 2def twoSum(numbers: 'List[int]', target: 'int') -> 'List[int]': 3 left, right = 0, len(numbers) - 1 4 while left < right: 5 if numbers[left] + numbers[right] == target: 6 return [left + 1, right + 1] 7 if numbers[left] + numbers[right] < target: 8 left += 1 9 else: 10 right -= 1 11 return [0, 0]
Let's make it a little more difficult. Upgrade the two-sum problem to four-sum.
Find all unique quadruplets in the array which gives the sum of target.
Idea: With the help of the above example, the solution is very clear. We can use the left and right boundary to find the solution space. We can divide the four-sum problem to three-sum, and finally to two-sum.
1#  https://leetcode.com/problems/4sum/ 2def fourSum(nums: 'List[int]', target: int) -> 'List[int]': 3 result, n = , len(nums) 4 if n < 4: return result 5 nums = sorted(nums) 6 if sum(nums[-4:]) < target: 7 return result 8 9 for i in range(n - 3): 10 # boundary checker, stop early 11 if sum(nums[i:i + 4]) > target: 12 break 13 # right boundary checker 14 if nums[i] + sum(nums[-3:]) < target: 15 continue 16 # skip same element, but keep the first one 17 if i > 0 and nums[i] == nums[i - 1]: 18 continue 19 # simplify the problem to three sum 20 target2 = target - nums[i] 21 for j in range(i + 1, n - 2): 22 if sum(nums[j:j + 3]) > target2 or sum(nums[-3:]) < target2: 23 break 24 if nums[j] + sum(nums[-2:]) < target2: 25 continue 26 if j > i + 1 and nums[j] == nums[j - 1]: 27 continue 28 # simplify the problem to two sum 29 target3 = target2 - nums[j] 30 left = j + 1 31 right = n - 1 32 while left < right: 33 if nums[left] + nums[right] == target3: 34 result.append([nums[i], nums[j], nums[left], nums[right]]) 35 while left < right and nums[left] == nums[left + 1]: 36 left += 1 37 while left < right and nums[right] == nums[right - 1]: 38 right -= 1 39 left += 1 40 right -= 1 41 elif nums[left] + nums[right] < target3: 42 left += 1 43 else: 44 right -= 1 45 return result
Here is an example in which we focus on the interval.
Find two lines, which together with x-axis form a container, such that the container contains the most water possible.
Idea: To help you understand, I've drawn an example diagram. We calculate the water area by the interval formed by the current left and right bounds.
1#  https://leetcode.com/problems/container-with-most-water/ 2def maxArea(height: 'List[int]') -> int: 3 left, right = 0, len(height) - 1 4 area_max = (right - left) * min(height[left], height[right]) 5 6 while left < right: 7 # judge which side decide the area (shorter height) 8 if height[left] <= height[right]: 9 last_height = height[left] 10 # skip all the height less than current 11 while height[left] <= last_height and left < right: 12 left += 1 13 else: 14 last_height = height[right] 15 # skip all the height less than current 16 while height[right] <= last_height and left < right: 17 right -= 1 18 19 if left >= right: 20 return area_max 21 area_max = max(area_max, (right - left) * min(height[left], height[right])) 22 return area_max
In some other scenarios, we need to compare in two sequences. Each pointer represents the current logical position in the corresponding sequence.
1# p1 & p2 from two sequences: p1-> p2-> 2def pointers_from_two_seq(self, seq1, seq2): 3 # init pointers 4 p1, p2 = 0, 0 # or seq1, seq2 5 # or other condition 6 while p1 < len(seq1) and p2 < len(seq2): 7 # p1 index moves when satisfy the condition 8 if self.p1_condition(p1): 9 p1 += 1 # or p1 = next(seq1) 10 # p2 index move when satisfy the condition 11 if self.p2_condition(p2): 12 p2 += 1 # or p2 = next(seq2) 13 # process logic before or after pointers movement 14 self.process_logic(p1, p2)
From the following example diagram, we can see that these two pointers are relatively independent. How they move forward is determined by their own strategies.
Let's look at an example with multiple sequences.
Design a class which receives a list of
wordsin the constructor and implements a method that takes two words,
word2, and returns the shortest distance between these two words in the list.
Idea: First, we create a group of index sequences,
locations, for each word. Every time
shortest function is called, we pick up the corresponding sequences
loc2. Then we use two pointers,
l2, to calculate the shortest distance. In each iteration, the smaller pointer moves one step forward.
1#  https://leetcode.com/problems/shortest-word-distance-ii/ 2class WordDistance: 3 def __init__(self, words: 'List[str]'): 4 self.locations = defaultdict(list) 5 # Prepare a mapping from a word to all it's locations (indices). 6 for i, w in enumerate(words): 7 self.locations[w].append(i) 8 9 def shortest(self, word1: str, word2: str) -> int: 10 loc1, loc2 = self.locations[word1], self.locations[word2] 11 l1, l2 = 0, 0 12 min_diff = float("inf") 13 14 # Until the shorter of the two lists is processed 15 while l1 < len(loc1) and l2 < len(loc2): 16 min_diff = min(min_diff, abs(loc1[l1] - loc2[l2])) 17 if loc1[l1] < loc2[l2]: 18 l1 += 1 19 else: 20 l2 += 1 21 return min_diff
Here is another example combined with backtracking.
Given an input string (s) and a pattern (p), implement wildcard pattern matching with support for '?' and '*'.
Idea: Assign one pointer
s_cur for the target string and another
p_cur for the pattern. The two pointers record the current match position. When matching '*',
star are the flags that help to backtrack if matching failed in greedy.
1#  https://leetcode.com/problems/wildcard-matching/ 2def isMatch(s: str, p: str) -> bool: 3 s_cur, p_cur, match, star = 0, 0, 0, -1 4 5 while s_cur < len(s): 6 # match sucess, both two pointers move forward 7 if p_cur < len(p) and (s[s_cur] == p[p_cur] or p[p_cur] == '?'): 8 s_cur += 1 9 p_cur += 1 10 # if matching star, record current state and try to match in greedy 11 elif p_cur < len(p) and p[p_cur] == '*': 12 match = s_cur 13 star = p_cur 14 p_cur += 1 15 # backtrack if match failed 16 elif star != -1: 17 p_cur = star + 1 18 match += 1 19 s_cur = match 20 else: 21 return False 22 # corner case: remove continuous star in the rest 23 while p_cur < len(p) and p[p_cur] == '*': 24 p_cur += 1 25 26 if p_cur == len(p): 27 return True 28 else: 29 return False
In this guide, we continued learning about two other types of the two pointers technique through templates and examples: left and right boundary and pointer-1 and pointer-2 from two sequences.
In the third part, we will continue to learn about other types of two pointers.
To access the complete code, you can download Algorithm Templates from Github. I hope some of them will be useful for you.
This guide is part of a series on the two pointers technique:
I hope you enjoyed it. If you have any questions, you're welcome to contact me at [email protected].