awelgorithms

Decide window versus two pointers

Updated

A window is two pointers on the original index line, both moving right, and the answer uses every index between L and R. Two pointers also means L from the left and R from the right on a sorted array, where the slice is not a contiguous original subarray.

What to focus on

Unique characters, covering, at-most-k: window. Two-sum after sort, container-from-ends: two pointers, not a sliding window of the input as given.

Worked examples

window unique

s = "abcabcbb"
  1. L and R both increase
  2. contents s[L..R] in original order

Result: window

two-sum sorted

a = [4, 1, 9, 1], t=10 after sort [1,1,4,9]
  1. L=0 R=3 on the sorted copy
  2. [1,9] is not a[0..3] of the original

Result: two pointers, not a window of a

product window

a = [1, 2, 3, 4], prod < 10
  1. L,R on the original
  2. middle 2 and 3 stay in [0,2]

Result: window, not inward ends

Try it

Write classifyPair(s). s is a string, or a is an array of integers. A window includes every original index from L through R, both moving right. Opposite-end two pointers on a sorted copy is not a window of the input. Print window or two-pointers. Return nothing. Unique "abcabcbb", two-sum 10 on sorted [4, 1, 9, 1], prod<10 on [1, 2, 3, 4], covering ADOBECODEBANC vs ABC.

s is nonempty. a has length ≥ 2. Product cases use positive integers. A target pair exists after sort.