find if two arrays contain the same set of integers without extra space and faster than NlogN

前端 未结 14 820
盖世英雄少女心
盖世英雄少女心 2020-12-02 17:27

I came across this post, which reports the following interview question:

Given two arrays of numbers, find if each of the two arrays have the same s

14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 18:00

    I'll assume that the integers in question are of fixed size (eg. 32 bit).

    Then, radix-quicksorting both arrays in place (aka "binary quicksort") is constant space and O(n).

    In case of unbounded integers, I believe (but cannot proof, even if it is probably doable) that you cannot break the O(n k) barrier, where k is the number of digits of the greatest integer in either array.

    Whether this is better than O(n log n) depends on how k is assumed to scale with n, and therefore depends on what the interviewer expects of you.

提交回复
热议问题