mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
docs(docs): add and expand algorithm documentation
This commit is contained in:
1 parent
34bb305a96
commit
32a41bdcb0
10 files changed
+1061
-50
No files matched your search
@@ -3,28 +3,99 @@ title: 'Hash Tables'
|
||||
description: 'In algorithms, arrays and strings are very similar. They are both ordered collections of elements. The difference is that arrays are mutable, while strings are immutable.'
|
||||
---
|
||||
|
||||
## The idea
|
||||
|
||||
Technically, an array can't be resized. A dynamic array, or list, can be. In the context of algorithm problems, usually when people talk about arrays, they are referring to dynamic arrays. In this entire course, we will be talking about dynamic arrays/lists, but we will just use the word "array".
|
||||
A hash table is like a helpful grocery clerk.
|
||||
You say "avocado". She tells you the price at once.
|
||||
She does not walk the aisles. She just *knows*.
|
||||
|
||||
## Time Complexity
|
||||
How? A hash function turns your key into a slot number.
|
||||
Same key → same slot, every time. Lookup is O(1).
|
||||
|
||||
Arrays and strings are both ordered collections of elements. The difference is that arrays are mutable, while strings are immutable.
|
||||
## The picture
|
||||
|
||||
| Operation | Array/List | String(Immutable) |
|
||||
| --------- | ---------- | ----------------- |
|
||||
| Appending to end | *O(1) | O(n) |
|
||||
| Popping from end | O(1) | O(n) |
|
||||
| Insertion, not from end | O(n) | O(n) |
|
||||
| Deletion, not from end | O(n) | O(n) |
|
||||
| Modifying an element | O(1) | O(n) |
|
||||
| Random access | *O(1) | O(n) |
|
||||
| Checking if an element exists | O(1) | O(n) |
|
||||
```mermaid
|
||||
flowchart LR
|
||||
K1["'apple'"] --> H["Hash<br/>function"]
|
||||
K2["'milk'"] --> H
|
||||
K3["'avocado'"] --> H
|
||||
H --> S0["Slot 0: milk → 1.49"]
|
||||
H --> S1["Slot 1: apple → 0.67"]
|
||||
H --> S2["Slot 2: avocado → 1.99"]
|
||||
|
||||
:::note[Clarification 1]
|
||||
Appending to the end of a list is amortized O(1) time complexity. This means that the time complexity of appending to the end of a list is the same as the time complexity of appending to the end of an array.
|
||||
:::
|
||||
style H fill:#1565c0,color:#fff
|
||||
```
|
||||
|
||||
:::note[Clarification 2]
|
||||
Random access in this context means that you can access an element at any index in constant time.
|
||||
:::
|
||||
## Three jobs it does
|
||||
|
||||
1. **Membership** — "have I seen this before?"
|
||||
2. **Frequency** — "how many times?"
|
||||
3. **Mapping** — "what goes with this?"
|
||||
|
||||
## Membership (LC 217 — Contains Duplicate)
|
||||
|
||||
```python
|
||||
def contains_duplicate(nums: list[int]) -> bool:
|
||||
seen = set()
|
||||
for n in nums:
|
||||
if n in seen:
|
||||
return True
|
||||
seen.add(n)
|
||||
return False
|
||||
```
|
||||
|
||||
## Frequency (LC 242 — Valid Anagram)
|
||||
|
||||
```python
|
||||
from collections import Counter
|
||||
|
||||
def is_anagram(s: str, t: str) -> bool:
|
||||
if len(s) != len(t):
|
||||
return False
|
||||
return Counter(s) == Counter(t)
|
||||
```
|
||||
|
||||
## Key design (LC 49 — Group Anagrams)
|
||||
|
||||
Sometimes the trick is *what you use as the key*.
|
||||
"eat", "tea", "ate" all sort to "aet". Sorted string = group key.
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
A["eat"] --> K["key: 'aet'"]
|
||||
B["tea"] --> K
|
||||
C["ate"] --> K
|
||||
K --> G["group: [eat, tea, ate]"]
|
||||
|
||||
style K fill:#1565c0,color:#fff
|
||||
```
|
||||
|
||||
```python
|
||||
from collections import defaultdict
|
||||
|
||||
def group_anagrams(strs: list[str]) -> list[list[str]]:
|
||||
groups = defaultdict(list)
|
||||
for s in strs:
|
||||
key = "".join(sorted(s))
|
||||
groups[key].append(s)
|
||||
return list(groups.values())
|
||||
```
|
||||
|
||||
## Complexity
|
||||
|
||||
| | Average | Worst |
|
||||
|---|---|---|
|
||||
| Lookup / insert / delete | O(1) | O(n) |
|
||||
|
||||
Worst case comes from collisions. Interviews treat it as O(1).
|
||||
|
||||
## Close-out ritual
|
||||
|
||||
Before you submit, say out loud:
|
||||
1. Time and space complexity.
|
||||
2. One edge case trace: empty input, all same element, or key collision on your key design.
|
||||
|
||||
## Plan problems
|
||||
|
||||
**A-set:** LC 217 · 242 · 1 · 49 · 347
|
||||
**B-set:** LC 383 · 205 · 128 · 290
|
||||
Reference in new issue
Block a user