Relative Content

Tag Archive for time-complexity

why is this fuction solving the two sum problem faster?

def two_sum_with_dic(numbers, target): complements = {} for i in range(len(numbers)): if numbers[i] in complements: return i, complements[numbers[i]] else: complements[numbers[i]] = target – numbers[i] def two_sum_fromcw(numbers, target): complements = [target – x for x in numbers] for i in range(len(numbers)): if numbers[i] in complements and complements.count(complements[i]) == 1: if complements.index(numbers[i]) != i: return i, numbers.index(complements[i]) elif […]

Question about time complexity caculation and ignoring

During the assignment, I was asked to describe the time complexity of my algorithm, and as I was writing, I suddenly had a question.
I know that we can usually ignore the coefficients before n and m in the time complexity calculation, but in the case of O(2n+4m), each coefficient is different, can we ignore them equally and still be bounded by O(n+m)?