Find unique common element from 3 arrays

前端 未结 7 1593
故里飘歌
故里飘歌 2021-02-03 14:43

Original Problem:
I have 3 boxes each containing 200 coins, given that there is only one person who has made calls from all of the three boxes and thus the

7条回答
  •  忘了有多久
    2021-02-03 15:25

    Let N = 200, k = 3,

    1. Create a hash table H with capacity ≥ Nk.

    2. For each element X in array 1, set H[X] to 1.

    3. For each element Y in array 2, if Y is in H and H[Y] == 1, set H[Y] = 2.

    4. For each element Z in array 3, if Z is in H and H[Z] == 2, return Z.

    5. throw new InvalidDataGivenByInterviewerException();

    O(Nk) time, O(Nk) space complexity.

提交回复
热议问题