Python 排序容器的时间复杂度

Python 排序容器的时间复杂度,python,time,complexity-theory,Python,Time,Complexity Theory,我使用Python的SortedDict容器解决了一个问题,我想知道获取最高密钥的时间复杂度是多少: from sortedcontainers import SortedDict treeMap = SortedDict() treeMap[1] = [4] treeMap[1].append(6) treeMap[3] = [9] print(treeMap.keys()[-1]) # get the highest key in the sorted dictionary, should

我使用Python的SortedDict容器解决了一个问题,我想知道获取最高密钥的时间复杂度是多少:

from sortedcontainers import SortedDict
treeMap = SortedDict()
treeMap[1] = [4]
treeMap[1].append(6)
treeMap[3] = [9]
print(treeMap.keys()[-1]) # get the highest key in the sorted dictionary, should be 3

我知道SortedDict中的大多数操作都是O(log(n)),但我对treeMap.keys()[-1]特别感到困惑。对于普通字典,在python3中d.keys()是O(1)。d.keys()[-1]O(1)也是吗?如果是sortedcontainers,是log(n)还是O(n)因为我需要访问最后一个元素?

sortedcontainers
sortedcontainers
在一个
sortedcontainers.SortedList
中维护封面下的钥匙顺序。因此,您所询问的操作实际上是
SortedList.\uu getitem\uuuu()
。发件人:

__getitem(索引)[来源] 在排序列表中的索引处查找值

sl.uu getitem_uu(索引)sl[索引]

支持切片

运行时复杂性:O(log(n))–近似值

现在是记录时间