insert, delete, max in O(1)

后端 未结 8 1165
北海茫月
北海茫月 2020-11-29 17:42

Can someone tell me which data structure supports insert/delete/maximum operation in O(1)?

8条回答
  •  青春惊慌失措
    2020-11-29 18:07

    @KennyTM's comment points out an important missing detail - insert where, and delete from where. So I am going to assume that you always want to insert and delete only from one end like a stack.

    Insertion (push) and Delete (pop) are O(1).

    To get Max in O(1), use an additional stack to record the current max which corresponds to the main stack.

提交回复
热议问题