Count the resizes
Standard · Write code · about 10 min
A hash table starts with size buckets. You insert the distinct keys of keys one by one. After each insert, if keys ÷ buckets is above 0.75, the number of buckets doubles.
Return how many times the table doubles.
Example 1
Input:
[4,[5,9,2,14,7,11]]
Output:
1
The 4th key makes the load 1.0, so the table doubles. 6 keys in 8 buckets is 0.75, not above.
Example 2
Input:
[2,[3,8,5,12,6,1,10]]
Output:
3
The table grows 2 → 4 → 8 → 16 at the 2nd, 4th and 7th key.
Constraints
- 1 ≤ size ≤ 1,000
- 0 ≤ keys.length ≤ 10,000
- Keys are distinct whole numbers ≥ 0
Learn the idea first: Load factor and resizing, a visual lesson.
More Load factor and resizing problems
- New positions after resize Warm-up
- Choose the threshold Warm-up
- Amortized cost Standard
- Total rehash moves Standard
- Implement resize Challenge