Click here to see the full article post
For the first num and the last num in the array, do they have the same "chance" to be switched?
@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.
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?
@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.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.