Loading
Loading
Trade memory for time by remembering what you have already seen.
A hash map or set answers "have I seen this?" and "how many times?" in expected constant time. Most quadratic scans exist because the algorithm keeps re-deriving information it already computed; storing that information once collapses the inner loop entirely.
Before writing a nested loop, ask what the inner loop is actually computing. If the answer is "does X exist among what I have seen" or "how many of X", a map removes the loop.
The shape, not a solution. Adapt the condition and the state to the problem in front of you.
def find_complement_pair(nums, target): """The canonical swap: a backwards search becomes a lookup.""" seen = {} for index, value in enumerate(nums): if target - value in seen: return [seen[target - value], index] seen[value] = index return [] def frequency(items): counts = {} for item in items: counts[item] = counts.get(item, 0) + 1 return counts def group_by_signature(words): """Group items sharing a derived key.""" groups = {} for word in words: key = "".join(sorted(word)) groups.setdefault(key, []).append(word) return list(groups.values())| Status | Problem | Difficulty | Role |
|---|---|---|---|
| First Unrepeated Symbol#13 | Easy | Primary | |
| Rearrangement Check#14 | Easy | Primary | |
| Matching Pair Sum#16 | Easy | Primary | |
| Group by Signature#17 | Medium | Primary | |
| Longest Consecutive Run#18 | Medium | Primary | |
| Duplicate Within Distance#20 | Easy | Primary | |
| Top Frequency Codes#21 | Medium | Related | |
| Word Chain#109 | Hard | Related | |
| Top Search Terms#115 | Medium | Related | |
| The Unpaired Badge#120 | Easy | Related | |
| Strongest XOR Pair#125 | Hard | Related | |
| Does the Digit Chain Settle?#126 | Easy | Related | |
| The Missing Ticket#127 | Easy | Related | |
| Divisible Stretches#149 | Medium | Related | |
| Plots With a Target Yield#150 | Hard | Related |
Sign in to keep a note here. It is free, and nobody else can see it.
Sign in