Given an integer array
nums
, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.Implement the
Solution
class:Solution(int[] nums)
Initializes the object with the integer arraynums
.
int[] reset()
Resets the array to its original configuration and returns it.
int[] shuffle()
Returns a random shuffling of the array.
Example 1:
Input ["Solution", "shuffle", "reset", "shuffle"] [[[1, 2, 3]], [], [], []] Output [null, [3, 1, 2], [1, 2, 3], [1, 3, 2]] Explanation Solution solution = new Solution([1, 2, 3]); solution.shuffle(); // Shuffle the array [1,2,3] and return its result. // Any permutation of [1,2,3] must be equally likely to be returned. // Example: return [3, 1, 2] solution.reset(); // Resets the array back to its original configuration [1,2,3]. Return [1, 2, 3] solution.shuffle(); // Returns the random shuffling of array [1,2,3]. Example: return [1, 3, 2]
Constraints:
1 <= nums.length <= 200
10
6
<= nums[i] <= 10
6
- All the elements of
nums
are unique.
- At most
5 * 10
4
calls in total will be made toreset
andshuffle
.
ํ์ด
์์ฐฌ
/** * @param {number[]} nums */ class Solution{ constructor(nums){ this.original = [...nums]; this.nums = nums; } reset() { this.nums = [...this.original]; return this.nums; } shuffle() { for(let i = this.nums.length - 1; i > 0; i--){ const random = Math.floor(Math.random() * (i + 1)); [this.nums[i], this.nums[random]] = [this.nums[random], this.nums[i]]; } return this.nums; } };
์ฌ์
โจ ํ์ด ๊ณผ์ ์ ๋ํด์ ์ค๋ช ํด๋ด ์๋ค.
๐ฅ ๋ฌธ์ ์ ํต์ฌ ์๊ณ ๋ฆฌ์ฆ
์๊ณ ๋ฆฌ์ฆ์ด๋ผ๊ธฐ๋ณด๋ค๋, ์์ด์ ์ด๋ป๊ฒ ํจ๊ณผ์ ์ผ๋ก ๋ฌธ์ ์ ์ ์ฉํ ์ ์๋๋๊ฐ ๊ด๊ฑด์ธ ๋ฌธ์ ์ด๋ค.
๐ฅ ์์ธ ํ์ด ๊ณผ์
- reset์ ์ํ originalNums ๋ฐฐ์ด์ ์์ฑํ๋ค.
- shuffle์์์ ๋ฌธ์ ๋ ๊ฒฐ๊ตญ, ์ ํ ์๊ฐ ์์ ์์ด์ ๊ณ์ํด์ ์กฐํํ๋ ๋ก์ง์ ์์ฑํ ์ ์๋์ง๋ค.
- ๊ธฐ์กด์ ์์ด์ ๋ง๋๋ ์๊ณ ๋ฆฌ์ฆ์ O(N!)์ด๋ค. ์ด๋ N = 4๋ฅผ ๋์ด๊ฐ์ ๋ ์คํ๋ ค O(N^2)๋ณด๋ค ๋นํจ์จ์ ์ด๋ค. ๋ฐ๋ผ์ ์ด๋ฅผ ์ต์ ํํด์ผ ํ๋ค.
- ์ธ๋ฑ์ค๋ฅผ ๋๋ค์ผ๋ก ์์ฑํด์, ๋ฐฐ์ด์ ์ค์์ ์ด์ฉํ๋ค. ์ด์ฐจํผ ์ ํ์ด ์ผ์ด๋ ๋๋ง๋ค, for๋ฌธ์ผ๋ก ์ค์ํ๋ฉฐ ์๋ก์ด ๋ฐฐ์ด์ ์์ฑํด๋ด๋ฉด, ์ฃผ์ด์ง ๋ฌธ์ ๋ฅผ O(N)๋ง์ ํด๊ฒฐํ ์ ์๋ค.
/** * @param {number[]} nums */ var Solution = function(nums) { this.nums = [...nums]; this.originalNums = [...nums]; return this; }; /** * @return {number[]} */ Solution.prototype.reset = function() { return this.originalNums; }; /** * @return {number[]} */ Solution.prototype.shuffle = function() { const { floor, random } = Math; for (let i = 0; i < this.nums.length; i += 1) { const randomIndex = floor(random() * this.nums.length); [this.nums[i], this.nums[randomIndex]] = [this.nums[randomIndex], this.nums[i]] } return this.nums }; /** * Your Solution object will be instantiated and called as such: * var obj = new Solution(nums) * var param_1 = obj.reset() * var param_2 = obj.shuffle() */
ํจ์ฑ
var Solution = function(nums) { this.nums = nums; this.arr = [...nums]; }; Solution.prototype.reset = function() { return this.nums; }; Solution.prototype.shuffle = function() { const arr = this.arr; arr.forEach((_, idx) => { const randomIdx = Math.floor(Math.random() * arr.length); [arr[randomIdx], arr[idx]] = [arr[idx], arr[randomIdx]]; }); return this.arr; };