--- 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] ] */ ```