Implement a stack that pops out the most frequently added item. Stack supports 3 functions - push, pop,and top. Give complexity of each functions in your implementation.
--------------------------------------------------------------
One
option is to combine heap (to get top) with hashtable (to find element in heap). That way insert is o(logn) and retrieval is o(1).
高效栈实现

本文介绍了一种结合堆与哈希表的高效栈实现方法,通过该方法可以实现插入操作的时间复杂度为O(log n),检索操作的时间复杂度为O(1)。
544

被折叠的 条评论
为什么被折叠?



