Find largest and second largest element in a range

后端 未结 11 2019
情歌与酒
情歌与酒 2020-12-10 07:30

How do I find the above without removing the largest element and searching again? Is there a more efficient way to do this? It does not matter if the these elements are dupl

11条回答
  •  隐瞒了意图╮
    2020-12-10 07:40

    You can scan the list in one pass and save the 1st and 2nd values, that has a O(n) efficiency while sorting is O(n log n).

    EDIT:
    I think that partial sort is O(n log k)

提交回复
热议问题