Random sequence iteration in O(1) memory?

前端 未结 6 1473
[愿得一人]
[愿得一人] 2021-02-19 07:58

Say you want to iterate over a sequence [0 to n] in a random order, visiting every element exactly once. Is there any way to do this in O(1) memory, i.e. without creati

6条回答
  •  面向向阳花
    2021-02-19 08:30

    If you could mutate the sequence in-place, you could simply repeatedly draw a random number from 0-N, and then erase the element you visited, or swap it to the end, or such schemes.

提交回复
热议问题