코딩 테스트/알고리즘 공부 22

숫자와 문자가 섞인 문자열 배열 정렬

.sort(({ properties: { name: a = 0 } } = {}, { properties: { name: b = 0 } } = {}) => (a.match(/\d+/) ?? [0])[0] - (b.match(/\d+/) ?? [0])[0]) .sort(({ properties: { name: a = '가' } } = {}, { properties: { name: b = '가' } } = {}) => (a.match(/[가-힣]/) ?? ['가'])[0] > (b.match(/[가-힣]/) ?? ['가'])[0] ? 1 : (a.match(/[가-힣]/) ?? ['가'])[0] < (b.match(/[가-힣]/) ?? ['가'])[0] ? -1 : 0 )

stirling formula(팩토리얼)

https://en.wikipedia.org/wiki/Stirling%27s_approximation Stirling's approximation - Wikipedia From Wikipedia, the free encyclopedia Approximation for factorials Comparison of Stirling's approximation with the factorial In mathematics, Stirling's approximation (or Stirling's formula) is an approximation for factorials. It is a good approximation, le en.wikipedia.org 팩토리얼은 stirling formula에 근사함 수가..

Tree

//! 1진트리 class Tree { //tree의 constructor를 생성 : tree의 자식 노드들을 엘리먼트로 담을 children 배열로 선언 constructor(value) { this.value = value this.children = [] } // 자식 노드 생성 메소드 구현 : new 키워드로 자식 노드를 생성 한 후, children 배열에 push insertNode(value) { const childNode = new Tree(value) this.children.push(childNode) } // tree에서 value값을 탐색하기 위한 메소드 구현 contains(value) { // 현재 노드의 value 값이 찾는 값과 일치한다면 true 반환 if (this.va..

순열, 중복순열, 조합, 중복조합

순열 function solution(arr, n) { let result = [] function DFS(n, tempArr) { if (n === 0) return result.push(tempArr) for (let i = 0; i < arr.length; i++) { if (!tempArr.includes(arr[i])) DFS(n - 1, tempArr.concat(arr[i])); } } DFS(n, []) return result } // n만 주어졌을 때 function solution(n) { let result = [] let N = n function DFS(n, tempArr) { if (n === 0) return result.push(tempArr) for (let i = 1; i

수학적 지식 모음

제곱근이 정수면 약수의 개수가 홀수이며 제곱근이 정수가 아니면 약수의 개수가 짝수다 자연수 n을 만들 수 있는 연속된 숫자의 합의 개수는 n의 약수중 홀수의 개수이다 (n이 15라면 1+2+3+4+5, 4+5+6, 7+8, 15 약수는 1,3,5,15) // 짝수의 합 const half = ~~(n/2); return half*(half+1); 삼각형의 두 변의 길이가 주어 졌을때 -> [a,b] 나머지 변의 길이가 될 수 있는 정수의 개수는 Math.min(...[a,b]) * 2 - 1 분자와 분모의 최대공약수로 약분하면 기약분수. 기약분수일때 분모의 소인수가 2와 5뿐이면 유한소수. n개 연속된 정수의 합으로 sum을 만들 수 있을때 연속된 정수 배열 구하기 function solution(n, ..

최소공배수(lcm)

기본 방법 function solution(arr) { let answer = 0; let i = Math.max(...arr); while (true) { if (arr.every((ele) => i % ele === 0)) { answer = i; break; } i++; } return answer; } 최소공배수는 각 요소로 나누었을때 나머지가 0인점을 이용해서 i를 1씩 늘려나가면서 모든 요소가 나눌때 나머지 0 인 i를 구함 i는 최소한 arr의 요소중 제일 큰 값 이상 더 빠른 방법 function lcm(arr) { return arr.reduce((a, b) => (a * b) / gcd(a, b)); } function gcd(a, b) { return a % b ? gcd(b, a %..