Permutation

--- title: 'Permutation' author: 'Hun Im' date: 2024-09-02T11:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- Permutation is a concept learned in high school mathematics. At that time, I memorized the formulas without really understanding what permutations were or when to use them (I guess that’s why I didn’t study well as a kid...) **What is a Permutation?** A permutation refers to arranging all the elements of a set while considering the order. In other words, it refers to all possible arrangements of n given elements in all possible orders. **Characteristics of Permutations** - In permutations, the order is important. For example, {1, 2, 3} and {3, 2, 1} are considered different permutations. - The number of permutations that can be made with n elements is calculated as n! (n factorial). For example, with 3 elements, the number of permutations is 3! = 3 Γ— 2 Γ— 1 = 6. **Applications of Permutations** - When you need to consider all possible orders (e.g., scheduling problems) - When you need to list all possible cases to make the optimal choice (e.g., finding the shortest path) **JS Function** ```js function getPermutations(arr) { let results = [] function permute(current, remaining) { if (remaining.length === 0) { results.push(current) } for (let i = 0; i < remaining.length; i++) { let next = current.concat(remaining[i]) let newRemaining = remaining.slice(0, i).concat(remaining.slice(i + 1)) permute(next, newRemaining) } } permute([], arr) return results } const array = [1, 2, 3] const permutations = getPermutations(array) console.log(permutations) /** [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] ] */ ```
Β 
--- title: 'μˆœμ—΄(Permutation)' author: 'μž„ν›ˆ' date: 2024-09-02T11:53:04+09:00 category: ['POSTS'] tags: ['Algorithm', 'Javascript'] og_image: "/images/gamer.png" keywords: ['Javascript', 'Algorithm'] --- μˆœμ—΄μ€ 고등학ꡐ μˆ˜ν•™ λ•Œ λ°°μš°λŠ” κ°œλ…μ΄λ‹€. κ·Έλ•ŒλŠ” λ¬΄μž‘μ • κ³΅μ‹λ§Œ μ™Έμ› κΈ° λ•Œλ¬Έμ—, μˆœμ—΄μ΄ 무엇인지, μ–Έμ œ μ“°μ΄λŠ”μ§€ λͺ°λžλ‹€.(μ–΄λ¦΄λ•Œ 곡뢀λ₯Ό 잘λͺ»ν–ˆλ˜ μ΄μœ μΈλ“― ...) **μˆœμ—΄μ΄λž€?** μˆœμ—΄(Permutation)은 집합에 μ†ν•œ λͺ¨λ“  μ›μ†Œμ˜ μˆœμ„œλ₯Ό κ³ λ €ν•˜μ—¬ λ°°μ—΄ν•˜λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€. 즉, μ£Όμ–΄μ§„ n개의 μ›μ†Œλ₯Ό λͺ¨λ‘ μ‚¬μš©ν•˜μ—¬ λ§Œλ“€ 수 μžˆλŠ” κ°€λŠ₯ν•œ λͺ¨λ“  μˆœμ„œμ˜ 쑰합을 λ§ν•©λ‹ˆλ‹€. **μˆœμ—΄μ˜ νŠΉμ§•** - μˆœμ—΄μ€ μˆœμ„œκ°€ μ€‘μš”ν•©λ‹ˆλ‹€. 즉, {1, 2, 3}κ³Ό {3, 2, 1}은 μ„œλ‘œ λ‹€λ₯Έ μˆœμ—΄λ‘œ μ·¨κΈ‰λ©λ‹ˆλ‹€. - n개의 μ›μ†Œλ‘œ λ§Œλ“€ 수 μžˆλŠ” μˆœμ—΄μ˜ κ°œμˆ˜λŠ” n! (n νŒ©ν† λ¦¬μ–Ό)둜 κ³„μ‚°λ©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄, 3개의 μ›μ†Œκ°€ μžˆμ„ λ•Œ μˆœμ—΄μ˜ κ°œμˆ˜λŠ” 3! = 3 Γ— 2 Γ— 1 = 6κ°œμž…λ‹ˆλ‹€. **μˆœμ—΄μ˜ ν™œμš©** - λͺ¨λ“  κ°€λŠ₯ν•œ μˆœμ„œλ₯Ό κ³ λ €ν•΄μ•Ό ν•  λ•Œ (예: 일정을 μ§œλŠ” 문제) - 경우의 수λ₯Ό λͺ¨λ‘ λ‚˜μ—΄ν•˜μ—¬ 졜적의 선택을 ν•΄μ•Ό ν•  λ•Œ (예: μ΅œλ‹¨ 경둜 μ°ΎκΈ°) **JS ν•¨μˆ˜** ```js function getPermutations(arr) { let results = [] function permute(current, remaining) { if (remaining.length === 0) { results.push(current) } for (let i = 0; i < remaining.length; i++) { let next = current.concat(remaining[i]) let newRemaining = remaining.slice(0, i).concat(remaining.slice(i + 1)) permute(next, newRemaining) } } permute([], arr) return results } const array = [1, 2, 3] const permutations = getPermutations(array) console.log(permutations) /** [ [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] ] */ ```