Two pointers and windows

Decide which pattern applies before coding: pointers from both ends, slow and fast, a fixed window, a window that grows and shrinks, or a running total.

The lesson is Two pointers and windows.

Reverse only the vowels

Write reverse_vowels(text) that returns the string with its vowels (a e i o u, either case) reversed in order while every other character stays in place.

reverse_vowels("hello")     ->  "holle"
reverse_vowels("leetcode")  ->  "leotcede"
reverse_vowels("xyz")       ->  "xyz"

The point is to practise two pointers moving inwards and skipping items that do not qualify.

def reverse_vowels(text):
    ...

Remove a value in place

Write remove_value(items, target) that moves every item that is not target to the front of the list, keeping their order, and returns how many there are. What is left in the list after that count does not matter. Change the list you are given.

items = [3, 2, 2, 3];  k = remove_value(items, 3)  ->  k == 2 and items[:2] == [2, 2]
items = [0, 1, 2, 2, 3, 0, 4, 2];  k = remove_value(items, 2)  ->  k == 5 and items[:5] == [0, 1, 3, 0, 4]

The point is to practise slow and fast pointers writing in place.

def remove_value(items, target):
    ...

Best average over k days

Write max_average(nums, k) that returns the largest average of any k consecutive values, as a float. The list has at least k values.

max_average([1, 12, -5, -6, 50, 3], 4)  ->  12.75     ((12 - 5 - 6 + 50) / 4)
max_average([5], 1)                     ->  5.0

The point is to practise a fixed-size sliding window. One check has a hundred thousand values with k of fifty thousand; recomputing each window's sum will not finish.

def max_average(nums, k):
    ...

Longest run of ones with k flips

bits is a list of 0s and 1s. Write longest_ones(bits, k) that returns the longest run of consecutive items you could make all 1 by flipping at most k zeros.

longest_ones([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2)  ->  6
longest_ones([0, 0, 0], 0)                          ->  0
longest_ones([0, 0], 5)                             ->  2

The point is to practise a variable window whose validity rule is "at most k zeros inside".

def longest_ones(bits, k):
    ...

Longest stretch with at most k distinct letters

Write longest_k_distinct(text, k) that returns the length of the longest run of consecutive characters containing at most k different characters. k is at least 1.

longest_k_distinct("eceba", 2)  ->  3     ("ece")
longest_k_distinct("aa", 1)     ->  2
longest_k_distinct("abcabc", 3) ->  6

The point is to practise a variable window whose state is a Counter of what is inside.

def longest_k_distinct(text, k):
    ...

Balance point

Write pivot_index(nums) that returns the smallest index where the sum of everything to its left equals the sum of everything to its right, or -1 if there is none. The ends count as having an empty side summing to 0.

pivot_index([1, 7, 3, 6, 5, 6])  ->  3
pivot_index([1, 2, 3])           ->  -1
pivot_index([2, 1, -1])          ->  0

The point is to practise a running left sum against a known total instead of two prefix lists.

def pivot_index(nums):
    ...

Trapped rainwater

heights describes a row of walls. After rain, water sits wherever a lower wall has higher walls somewhere on both sides. Write trapped(heights) that returns the total units of water held.

trapped([0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1])  ->  6
trapped([4, 2, 0, 3, 2, 5])                     ->  9
trapped([1, 2, 3])                              ->  0

The point is to practise two pointers where you can safely settle the lower side: the water above a position is bounded by the smaller of the tallest walls to its left and right.

def trapped(heights):
    ...

All distinct pairs with a sum

items is sorted and may contain repeats. Write pairs_with_sum(items, target) that returns every distinct pair [a, b] with a <= b and a + b == target, in increasing order of a. Each distinct pair appears once even if the values repeat.

pairs_with_sum([1, 1, 2, 3, 3, 4, 5], 6)  ->  [[1, 5], [2, 4], [3, 3]]
pairs_with_sum([1, 2], 10)                ->  []

The point is to practise two pointers with a skip past repeated values after a match.

def pairs_with_sum(items, target):
    ...

Is it a subsequence?

Write is_subsequence(small, big) that returns True if the characters of small appear in big in the same order, not necessarily next to each other.

is_subsequence("ace", "abcde")  ->  True
is_subsequence("aec", "abcde")  ->  False
is_subsequence("", "abc")       ->  True

The point is to practise two pointers over two different sequences.

def is_subsequence(small, big):
    ...

Merge into the back of a buffer

a is a list whose first m items are sorted and whose remaining len(b) slots are zeros, used as spare room. b is a sorted list. Write merge_into(a, m, b) that merges b into a in place so that all of a is sorted, and returns a.

merge_into([1, 2, 3, 0, 0, 0], 3, [2, 5, 6])  ->  [1, 2, 2, 3, 5, 6]
merge_into([0], 0, [1])                       ->  [1]
merge_into([2, 0], 1, [1])                    ->  [1, 2]

The point is to practise filling from the back so that nothing is overwritten before it is used.

def merge_into(a, m, b):
    ...

Next: Linked lists.