384. Shuffle an Array
brute force
public int[] shuffle() {
List<Integer> aux = getArrayCopy();
for (int i = 0; i < array.length; i++) { // O(n)
int removeIdx = rand.nextInt(aux.size());
array[i] = aux.get(removeIdx);
aux.remove(removeIdx); // this is also O(n)
}
return array;
}use fisher yates shuffle algorithm
latest explain
Last updated
