[JS] Shuffle an Array
2022. 9. 28. 01:17
🔒 문제 (LeetCode 384)
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.
Constraints:
- 1 <= nums.length <= 50
- -106 <= nums[i] <= 106
- All the elements of nums are unique.
- At most 104 calls in total will be made to reset and shuffle.
🌊 입출력
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]
🔑 해결
🌌 알고리즘
/**
* @param {number[]} nums
*/
var Solution = function(nums) {
this.nums = nums
};
/**
* @return {number[]}
*/
Solution.prototype.reset = function() {
return this.nums
};
/**
* @return {number[]}
*/
Solution.prototype.shuffle = function() {
const random = [...this.nums]
const swap = (i, j) => {
[random[i], random[j]] = [random[j], random[i]]
}
for(let i = 0; i < random.length; i++) {
const randIdx = Math.floor(Math.random() * random.length)
swap(i, randIdx)
}
return random
};
/**
* 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()
*/
class 사용
class Solution {
constructor(nums) {
this.nums = nums;
this.random = [...nums];
}
reset() {
return this.nums;
}
shuffle() {
for(let i = 0; i < this.random.length; i++) {
const randIdx = Math.floor(Math.random() * this.random.length);
this.swap(i, randIdx);
}
return this.random;
}
swap(i, j) {
[this.random[i], this.random[j]] = [this.random[j], this.random[i]]
}
}
'코딩테스트 (JS) > ETC' 카테고리의 다른 글
[JS] Maximum Consecutive Floors Without Special Floors (0) | 2022.10.08 |
---|---|
[JS] Happy Number (0) | 2022.09.28 |
[JS] Jump Game (0) | 2022.09.04 |
[JS] Single Number (0) | 2022.08.03 |
[JS] Reverse Bits (0) | 2022.08.03 |