Searching two arrays for matches, no extra memory

后端 未结 4 504
暖寄归人
暖寄归人 2021-02-02 16:09

I had an interview the other day with Amazon, and a question they asked me was pertaining to the following problem.

Given 2 integer arrays, containing any number of elem

4条回答
  •  离开以前
    2021-02-02 16:19

    One possible answer is similar to the HashMap solution... IF you know that the integers are within a very small window. It would be similar to this: http://en.wikipedia.org/wiki/Bucket_sort

    Basically, if the integers are guaranteed to be within a certain constant size window (i.e. all of them are 1-1000) then you can do it in constant space by incrementing each cell of index = whatever your number is. This is exactly the same as the HashMap solution, except you don't need to be able to account for all possible integers like a HashMap can, which lets you save on space. If this is unclear let me know in the comments and I'll explain further.

提交回复
热议问题