Find duplicate in an array

后端 未结 5 1832
醉梦人生
醉梦人生 2021-01-14 16:45

Given a read only array of n + 1 integers between 1 and n, find one number that repeats in linear time using less than O(n) space and traversing the stream sequentially O(1)

5条回答
  •  死守一世寂寞
    2021-01-14 17:28

    std::vector is a bitset, so it will use n bits. In Big-O notation, O(n/8)=O(n), that means the space is not less than O(n).

    I assume they do not look at the actual program, but only measure its space consumption in some example runs. So, using a bit vector tricks it into believing that it is better than O(n).

    But I agree with you. It should not be accepted.

提交回复
热议问题