Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created April 1, 2020 04:49
Show Gist options
  • Select an option

  • Save wushbin/b055950eb10833d4817032d63cc98d0c to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/b055950eb10833d4817032d63cc98d0c to your computer and use it in GitHub Desktop.
class Solution {
Map<Integer, Integer> shuffle;
int n;
Random rand;
int n_rows;
int n_cols;
public Solution(int n_rows, int n_cols) {
this.n = n_rows * n_cols;
this.shuffle = new HashMap<>();
this.rand = new Random();
this.n_rows = n_rows;
this.n_cols = n_cols;
}
public int[] flip() {
int idx = this.rand.nextInt(n--);
int pick = this.shuffle.getOrDefault(idx, idx);
this.shuffle.put(idx, this.shuffle.getOrDefault(n, n));
//this.shuffle[n] = pick; // no need;
return new int[]{pick / n_cols, pick % n_cols};
}
public void reset() {
this.n = n_rows * n_cols;
this.shuffle.clear();
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n_rows, n_cols);
* int[] param_1 = obj.flip();
* obj.reset();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment