O(N+M) time complexity

后端 未结 3 1132
孤街浪徒
孤街浪徒 2021-02-12 09:52

I\'m working through some practice problems where I\'m given a target time complexity and space complexity. One of them gives a target time complexity of O(N+M). I\'m having som

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-12 10:33

    So, in order to expand the other replies, I will try to add a example of such problems to help you understand:

    • Find a min/max in a N sized array, and then look for this value in an M sized array. Since you need to perform first min/max search, you cannot do it at once.

    For instance, summing up the elements of 2 vectors can be done in O(M+N), but it can be thought as O(N) (assuming N>M) or O(M) (if M>N).

提交回复
热议问题