본문 바로가기

Algorithm

(76)
[프로그래머스] 가장 먼 노드 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 const solution = (common) => { // 등차수열인지 등비수열인지 판별 const first = common[0]; const second = common[1]; const third = common[2]; const difference1 = second - first; const difference2 = third - second; // 다음에 올 수 let nextNumber = common[common.length - 1]; // 두 차이가 같으면 등차수열 if (difference1 === difference2) { const difference = difference1; ne..
[프로그래머스] 코딩테스트 입문 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 프로그래머스의 Lv.0 문제 풀이. 문제를 읽고 바로 풀 수 있을 정도로 매우 쉬운난이도로 화살표 함수를 사용하여 최대한 간략하게 풀이. 1. 두 수의 차 const solution = (num1, num2) => num1 - num2; 2. 두 수의 곱 const solution = (num1, num2) => num1 * num2; 3. 두 수의 합 const solution = (num1, num2) => num1 + num2; 4. 숫자 비교하기 const solution = (num1, num2) => num1 === num2 ? 1 : -1; 5. 몫 구하기 const solution = (num1, num2)..
자료구조와 알고리즘 - 재귀 함수 본 내용은 프로그래머스의 코딩테스트 광탈 방지 A to Z : JavaScript 강의를 토대로 작성하였습니다. 1. 재귀 함수란? 자기 자신을 호출하는 함수 재귀 호출(Recursion call) 함수 호출은 Call Stack에 쌓이므로, 스택 자료구조와 유사하게 동작 함수형 프로그래밍에선 루프 구현을 재귀로 구현 무한 루프에 빠지지 않도록 주의 2. 자바스크립트에서의 재귀 함수 콜 스택에 제한 자바스크립트 엔진마다 제한 수는 다름 크롬의 경우 1만개 꼬리 재구(Tail recursion)가 제공되지 않음 성능이 좋지 않음 3. 재귀로 구현해야 편한 알고리즘 Union-Find DFS Backtracking 스택을 통해서 구현할 수도 있지만, 코딩테스트에서는 빨리 푸는 것이 중요하므로 추천하지 않음 ..
[프로그래머스] 가장 먼 노드 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 // Queue 구현 class Queue { constructor() { this.queue = []; this.front = 0; this.rear = 0; } enqueue(value) { this.queue[this.rear++] = value; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } isEmpty(){ return this.rear === this.front; } } ​ function solution(n, edge) { // 트..
[프로그래머스] 입국심사 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 function solution(n, times) { // 모든 사람이 심사를 받는데 걸리는 시간의 최솟값은 1분 ~ (times에서 가장 큰 수 * n)분의 범위에 속함 // 가능한 최솟값 범위의 초기화 let min = 1; let max = Math.max(...times) * n; // 이진탐색 while (min acc + Math.floor(mid / cur), 0); if (sum
[프로그래머스] 배상 비용 최소화 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 class MaxHeap { constructor() { this.heap = [null]; } push(value) { this.heap.push(value); let currentIndex = this.heap.length - 1; let parentIndex = Math.floor(currentIndex / 2); while (parentIndex !== 0 && this.heap[parentIndex]
[프로그래머스] 성격 유형 검사하기 - JavaScript 풀이 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 // 성격 유형별 점수를 기록하는 객체 const scores = { 'R': 0, 'T': 0, 'C': 0, 'F': 0, 'J': 0, 'M': 0, 'A': 0, 'N': 0, }; ​ function solution(survey, choices) { var answer = ''; // 주어진 survey, choices 순회 survey.forEach((sur, idx) => { // 비동의 성격 유형 const first = sur[0]; // 동의 성격 유형 const second = sur[1]; // 선택한 점수 const choice = choices[idx]; if (choice 4)..
[프로그래머스] 프린터 - JavaScript 풀이(개선된 풀이) 본 게시물은 프로그래머스의 연습 문제 풀이입니다. 저작권은 (주) 그랩에게 있습니다 자바스크립트 코드 class Queue { constructor() { this.queue = []; this.front = 0; this.rear = 0; } enqueue(value) { this.queue[this.rear] = value; this.rear += 1; } dequeue() { const value = this.queue[this.front]; delete this.queue[this.front]; this.front += 1; return value; } peek() { return this.queue[this.front]; } } ​ function solution(priorities, locat..