当前位置: 首页 > 文档资料 > 算法珠玑 >

linear-list/linked-list/all-o1-data-structure

优质
小牛编辑
129浏览
2023-12-01

All O(1) Data Structure

描述

Implement a data structure supporting the following operations:

  1. Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is 1. guaranteed to be a non-empty string.
  2. Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string.
  3. GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "".
  4. GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string "".

Challenge: Perform all these in O(1) time complexity.

分析

LRU Cache很类似,用两个 HashMap 和一个双向链表。

代码

// TODO
// TODO

相关题目