Finding an appropriate data structure

前端 未结 3 880
离开以前
离开以前 2021-01-24 14:19

I have N keys.

I need to find a data structure which i can do with the following operations :

  1. building it in O(N)

  2. finding min in O(1)

3条回答
  •  天命终不由人
    2021-01-24 14:37

    When you say building in O(n), do you mean that addition has to be O(n), or that you have to build a collection of elements in O(n) such that addition has to be O(1)?

    You could augment pretty much any data structure with an extra reference to retrieve the minimal element in constant time.

    For #3, it sounds like you need to be able to find the median in O(lg n) and delete in O(1), or vice versa.

    For #4, you didn't specify the time complexity.

    To other posters - this is marked as homework. Please give hints rather than posting the answer.

提交回复
热议问题