Skip to content

visual walkthrough

All O`one Data Structure

HardHash Map + Doubly Linked BucketsReported at: AmazonLinkedInVMware

O(1) inc/dec/min/max with a bucket list.

Solve on LeetCode

The idea

Counts only ever change by one. So if keys are grouped into buckets by count and the buckets are kept in order, a key always moves to the bucket right next to its current one.

With the buckets in a doubly linked list, the smallest count is at the head and the largest at the tail: min and max are instant.

Complexity

approachtimespace
Map and scanO(n) for max/minO(n)
Linked list of count bucketsO(1) per opO(n)

More walkthroughs