Finding an appropriate data structure

前端 未结 3 839
离开以前
离开以前 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:50

    A popular question asked in Data Structures 1 exams/hws/tutorials. I'll try to give you some hints, if they don't suffice, comment, and I'll give you more hints.

    1. Remember that you don't have to use just one data structure, you can use several data structures.
    2. Recall the definition of a median: n/2 of the numbers are larger, and n/2 of the numbers are smaller
    3. What data structures do you know that are built in O(n), and complex operations on them are O(logn) or less? - Reread the tutorials slides on these data structures.
    4. It might be easier for you to solve 1+3 seperately from 1+2, and then think about merging them.

提交回复
热议问题