Created
April 1, 2020 04:49
-
-
Save wushbin/b055950eb10833d4817032d63cc98d0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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