我正在尝试确定所编写函数的空间和时间复杂度,以便可以在报告中根据渐近边界讨论该函数。
给出一个列表列表,其中每个子列表代表“冲突”中的两个元素,此函数返回 unique 成对冲突的字典。在这种情况下,[1,2]
和[2,1]
是彼此重复的,因此列表[[1,2], [1,2], [2,1], [3,4]]
将产生字典{1: [2], 3: [4]}
def calculateDistinctConflicts():
conflicts = [[1,2], [1,2], [2,1], [3,4]]
distinctConflicts = {}
for pair in conflicts:
element0 = pair[0]
element1 = pair[1]
conflictKeys = distinctConflicts.keys()
if element0 not in conflictKeys and element1 not in conflictKeys:
distinctConflicts[element0] = []
targetList = distinctConflicts[element0]
targetList.append(element1)
elif element0 in conflictKeys and element1 not in distinctConflicts[element0]:
targetList = distinctConflicts[element0]
targetList.append(element1)
elif element1 in conflictKeys and element0 not in distinctConflicts[element1]:
targetList = distinctConflicts[element1]
targetList.append(element0)
elif element0 in conflictKeys and element1 in distinctConflicts[element0] or element1 in conflictKeys and element0 in distinctConflicts[element1]:
continue
print (distinctConflicts)
# {1: [2], 3: [4]}