design

design

432.All-O-one-Data-Structurearrow-up-right (H) 380.Insert-Delete-GetRandom-O(1)arrow-up-right (M+) 381.Insert-Delete-GetRandom-O1-Duplicates-allowedarrow-up-right (H-) 716.Max-Stackarrow-up-right (M+) 355.Design-Twitterarrow-up-right (H) 535.Encode-and-Decode-TinyURLarrow-up-right (M) 631.Design-Excel-Sum-Formulaarrow-up-right (H) 642.Design-Search-Autocomplete-Systemarrow-up-right (M+) 895.Maximum-Frequency-Stackarrow-up-right (H) 1146.Snapshot-Arrayarrow-up-right (H) 1172.Dinner-Plate-Stacksarrow-up-right (H) 1381.Design-a-Stack-With-Increment-Operationarrow-up-right (H-) 1352.Product-of-the-Last-K-Numbersarrow-up-right (M+) 1418.Display-Table-of-Food-Orders-in-a-Restaurantarrow-up-right (H-) 1622.Fancy-Sequencearrow-up-right (H+)

Cache - [TODO]

  • 146.LRU-Cachearrow-up-right (H-)\

    • Brute force: Use a single dictionary impl, key -> (value, timestamp)

      • Get: O(1)

      • Set: O(n) because need to pop out elements if exceed maximum capacity

    • Complexity optimal: Dictionary + LinkedList

      • Get: O(1)

      • Set: O(1)

    • Simplest: Use the Python bulit-in OrderedDict impl (not SortedDict which order items based on keys) https://www.kunxi.org/2014/05/lru-cache-in-python/

      • Get: O(1)

      • Set: O(1)

  • 460.LFU Cachearrow-up-right (H)\

    • Brute force: Use a single dictionary impl, key -> (value, frequency)

      • Get: O(1)

      • Set: O(nlogn)

    • Direct inherit from LRU: Dictionary + linkedlist. Sort linkedlist using bubblesort https://www.kunxi.org/2016/12/lfu-cache-in-python/

      • Get: O(1)

      • Set: O(N) in LRU there is no sorting needed, but in LFU there is.

    • Dictionary + BST tree:

      • Get: O(1) + log(N) because BST needs to be balanced

      • Set: O(1) + log(N) because BST needs to delete element

    • MY original solution: https://www.kunxi.org/2016/12/lfu-cache-in-python/

      • One dictionary: key -> freq, another dictionary freq -> defaultdict(ordereddict)

      • Get: O(1)

      • Set: O(1)

Last updated