Longest chain
Standard · Write code · about 10 min
A hash table has size buckets and hashes a key with key mod size. Insert all distinct keys in keys.
Return the length of the longest chain, which is the worst case for one search. Return 0 when there are no keys.
Example 1
Input:
[[12,7,3,22,9],5]
Output:
3
Chain 2 holds 12, 7 and 22.
Example 2
Input:
[[1,2,3],5]
Output:
1
Constraints
- 0 ≤ keys.length ≤ 10,000
- keys are distinct, 0 ≤ key ≤ 1,000,000
- 1 ≤ size ≤ 1,000
Learn the idea first: Hash tables with separate chaining, a visual lesson.
More Hash tables with separate chaining problems
- Insert a sequence Warm-up
- Average chain length Warm-up
- Implement get and put Standard
- Count collisions Standard
- Implement remove and iteration Challenge