Difference between O(m+n) and O(mn)?

后端 未结 3 1343
梦如初夏
梦如初夏 2020-12-30 15:47

I was trying to find the complexities of an algorithm via different approaches. Mathematically I came across one O(m+n) and another O(mn) approach. However I am unable to gr

3条回答
  •  清歌不尽
    2020-12-30 16:36

    O(m+n) is much (an order of magnitude) faster than O(mn).

    The O(m+n) algorithm could be one that iterates 2 sets and does a constant time (O(1)) operation on each element.

    The O(mn) algorithm could be one that iterates the first set and does a linear search (O(n)) for the matching element in the second set.

    The O(mn) algorithm is probably what professors would call The Naive Approach

提交回复
热议问题