The pattern map: which signals point to which technique
🌍 In real life. A doctor doesn't try every medicine. Fever and a rash point one way, a cough and a wheeze another, and the patient's age rules some treatments out. Problems are the same: their wording carries clues, and each clue points to the techniques of the earlier chapters.
The idea in one sentence: read the problem for clues (sorted input, "pair", "stretch of items", "fewest moves", "count the ways", the size of n), let each clue vote for the techniques it fits, and try first the one that most clues agree on.
Its words:
- clue: a word or a limit in the problem that hints at a technique: "sorted", "pair", n ≤ 100,000
- pattern: a known technique with a known cost: two pointers, BFS, dynamic programming…
- subarray vs subsequence: items next to each other, or items in order with gaps allowed
The map:
- sorted input → binary search (Chapter 43) or two pointers (Chapter 36)
- two items adding up to a target → hash map (Chapter 35) or two pointers
- a stretch of items next to each other → sliding window (Chapter 37) or prefix sums (Chapter 38)
- the next bigger item → stack (Chapter 39)
- fewest moves, each the same cost → BFS (Chapter 51); different costs → Dijkstra (Chapter 54)
- which things are connected → union-find (Chapter 53) or DFS
- the k biggest, or the smallest again and again → heap (Chapter 49)
- every subset or order → backtracking (Chapter 46)
- count the ways, or the best total, with the same small questions repeating → dynamic programming (Chapters 57–59)
- meetings or intervals → sort, then greedy (Chapter 56)
- words by their first letters → trie (Chapter 50)
- n ≤ 20 allows brute force; n up to 100,000 rules it out (Lesson 61.1)
Step by step:
- underline the clues in the statement and the limits
- let each clue light up its techniques, and cross out the ones the size rules out
- try the technique most clues agree on; check it against the examples
Watch the clues of "a sorted list of 100,000 prices: do two of them add up to the budget?" vote:
A different problem, "list every way to pick dishes for a party menu, from at most 15 dishes", sends its votes elsewhere:
Why clues work. Each technique exists because of a structure: binary search needs sorted order to throw away half; a sliding window needs the items to be next to each other; DP needs small questions that repeat. A clue in the statement is that structure showing through, so it tells you which tools can even apply.
What it costs, by counting. Choosing well changes the cost more than any tidying of code: for the sorted prices, checking every pair is 4,999,950,000 steps, while two pointers take at most 100,000.
CLUES = {
"sorted": "binary search",
"subarray": "sliding window",
"shortest": "BFS",
"ways": "dynamic programming",
"largest": "heap",
"all subsets": "backtracking",
}
def suggest(problem):
text = problem.lower() # "Sorted" and "sorted" are the same clue
return [technique for word, technique in CLUES.items() if word in text]
print(suggest("Given a sorted list, find the target"))
print(suggest("Count the ways to climb n stairs"))
print(suggest("Find the largest sum of any subarray"))Mistakes and edge cases.
- one clue alone: "sorted" points to two techniques; read the rest of the problem before choosing
- subarray or subsequence: a subarray is next-to-each-other (window, prefix sums); a subsequence may skip items (often DP)
- weighted or not: BFS is right only when every move costs the same
- ignoring the size: a technique that fits the wording can still be too slow for the limits
Quiz
"Count the number of ways to climb n stairs, 1 or 2 at a time." Which technique?
- Dynamic programming
- Binary search
- A heap
"The quickest drive between two towns; each road has its own length in km." Which technique?
- Dijkstra
- BFS
- Sliding window
"The longest stretch of days with at most 2 rainy days in it." Which technique?
- Sliding window
- Backtracking
- Union-find
"The 10 most viewed videos out of 1,000,000." Which technique?
- A heap of size 10
- Trying every subset
- BFS
What does this print?
CLUES = { "sorted": "binary search", "subarray": "sliding window", "shortest": "BFS", "ways": "dynamic programming", "largest": "heap", "all subsets": "backtracking", } def suggest(problem): text = problem.lower() # "Sorted" and "sorted" are the same clue return [technique for word, technique in CLUES.items() if word in text] print(suggest("The SHORTEST route, and the largest bus"))suggest("Sorted list: find the target") should give ['binary search'], but gives []. Which line is wrong?
CLUES = {"sorted": "binary search", "ways": "dynamic programming"} def suggest(problem): text = problem return [t for word, t in CLUES.items() if word in text]
Coding tasks
Write
suggest(problem)with theCLUESdict in the starter: return the techniques whose clue word appears anywhere inproblem, in the dict's order, whatever the capital letters.Write
has_pair_with_sum(nums, k):numsis not sorted and can hold 100,000 numbers. ReturnTrueif two different positions hold numbers that add up tok. Pick the technique from the map.