Shuffle an Array

  • 1

    Click here to see the full article post

  • 0

    For the first num and the last num in the array, do they have the same "chance" to be switched?

  • 0

    @MindGeek I'm not super sure that I understand your question. However, if you can convince yourself that the brute force analysis is correct, it might help to think about how Fisher-Yates is actually almost identical to it. Essentially, after the ith iteration of the swapping loop, array[:i] (inclusive) is the result (so far) of the algorithm, while array[i+1:] is the "hat", or aux from the brute force solution.

  • 0

    I have a question about "we generate a random integer between the current index and the last index of the array" . Can we use a random integer within the range of 0 and the current index+1. i.e. rand.nextInt(i+1) in the shuffle method?? @emptyset Is there any advantage of using current index and the last index of the array? Like mathematicaly?

  • 0

    @PremiumLab_ There is no mathematical difference. It is possible that one method is superior by a constant factor due to some low-level CPU reason, but certainly not significantly.

  • 0

    In the second solution for reset, shouldn't we return array instead of original? We did that in the first solution, but why differently in second solution?

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.