Shuffle an Array

Deadline
Mar 5, 2022
Status
Archived
Type
Array
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 array nums.
  • 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
  • 106 <= nums[i] <= 106
  • All the elements of nums are unique.
  • At most 5 * 104 calls in total will be made to reset and shuffle.
 

ํ’€์ด

์€์ฐฌ
/** * @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; } };
์žฌ์˜

โœจ ํ’€์ด ๊ณผ์ •์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•ด๋ด…์‹œ๋‹ค.

๐Ÿ’ฅ ๋ฌธ์ œ์˜ ํ•ต์‹ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜
์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ธฐ๋ณด๋‹ค๋Š”, ์ˆœ์—ด์„ ์–ด๋–ป๊ฒŒ ํšจ๊ณผ์ ์œผ๋กœ ๋ฌธ์ œ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋А๋ƒ๊ฐ€ ๊ด€๊ฑด์ธ ๋ฌธ์ œ์ด๋‹ค.
 
๐Ÿ”ฅ ์ƒ์„ธ ํ’€์ด ๊ณผ์ •
  1. reset์„ ์œ„ํ•œ originalNums ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
  1. shuffle์—์„œ์˜ ๋ฌธ์ œ๋Š” ๊ฒฐ๊ตญ, ์ œํ•œ ์‹œ๊ฐ„ ์•ˆ์— ์ˆœ์—ด์„ ๊ณ„์†ํ•ด์„œ ์กฐํšŒํ•˜๋Š” ๋กœ์ง์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋‹ค.
  1. ๊ธฐ์กด์— ์ˆœ์—ด์„ ๋งŒ๋“œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ O(N!)์ด๋‹ค. ์ด๋Š” N = 4๋ฅผ ๋„˜์–ด๊ฐ”์„ ๋•Œ ์˜คํžˆ๋ ค O(N^2)๋ณด๋‹ค ๋น„ํšจ์œจ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ์ตœ์ ํ™”ํ•ด์•ผ ํ•œ๋‹ค.
  1. ์ธ๋ฑ์Šค๋ฅผ ๋žœ๋ค์œผ๋กœ ์ƒ์„ฑํ•ด์„œ, ๋ฐฐ์—ด์˜ ์Šค์™‘์„ ์ด์šฉํ–ˆ๋‹ค. ์–ด์ฐจํ”ผ ์…”ํ”Œ์ด ์ผ์–ด๋‚  ๋•Œ๋งˆ๋‹ค, 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; };