본문 바로가기

728x90

Algorithm

[NodeJS] 백준 9251번 - LCS /** * @link https://www.acmicpc.net/problem/9251 * @description * LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, 모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다. * 예를 들어, ACAYKP와 CAPCAK의 LCS는 ACAK가 된다. */ const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const input = []; rl.on('line', (line) => { input.push(line); })... 더보기
[NodeJS] 백준 1912번 - 연속합 /** * @link https://www.acmicpc.net/problem/1912 * @description * * n개의 정수로 이루어진 임의의 수열이 주어진다. 우리는 이 중 연속된 몇 개의 수를 선택해서 구할 수 있는 합 중 가장 큰 합을 구하려고 한다. 단, 수는 한 개 이상 선택해야 한다. * 예를 들어서 10, -4, 3, 1, 5, 6, -35, 12, 21, -1 이라는 수열이 주어졌다고 하자. 여기서 정답은 12+21인 33이 정답이 된다. */ const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const in.. 더보기
[NodeJS] 백준 24416번 /** * @link https://www.acmicpc.net/problem/24416 * @description * * * 오늘도 서준이는 동적 프로그래밍 수업 조교를 하고 있다. 아빠가 수업한 내용을 학생들이 잘 이해했는지 문제를 통해서 확인해보자. * * 오늘은 n의 피보나치 수를 재귀호출과 동적 프로그래밍으로 구하는 알고리즘을 배웠다. 재귀호출에 비해 동적 프로그래밍이 얼마나 빠른지 확인해 보자. 아래 의사 코드를 이용하여 n의 피보나치 수를 구할 경우 코드1 코드2 실행 횟수를 출력하자. * * 피보나치 수 재귀호출 의사 코드는 다음과 같다. * * fib(n) { * if (n = 1 or n = 2) * then return 1; # 코드1 * else return (fib(n - 1) +.. 더보기
[NodeJS] 백준 2565 - 전깃줄 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const input = []; rl.on('line', (line) => { input.push(line); }).on('close', () => { const [strN, ...strArr] = input; const numArr = strArr .map((v) => v.split(' ').map(Number)) .sort((a, b) => a[0] - b[0]); const result = func(Number(strN), numArr); console.log(result); .. 더보기
[Node.js]백준 11054번 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const input = []; rl.on('line', (line) => { input.push(line); }) .on('close', () => { const [, arr] = input; const nums = arr.split(' ').map(Number); const N = nums.length; const upMemo = new Array(N).fill(1); // LIS const downMemo = new Array(N).fill(1); // LDS for(let i .. 더보기
[Node.js]백준 11053번 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const input = []; rl.on('line', (line) => { input.push(line); }) .on('close', () => { const [, arr] = input; const nums = arr.split(' ').map(Number); const memo = new Array(nums.length + 1).fill(1); for(let i = 1; i < nums.length; i++) { let temp = 0; for(let j = 0; j < i;.. 더보기
[Node.js]백준 2156번 - 포도주 시식 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const input = []; rl.on('line', (line) => { input.push(Number(line)); }) .on('close', () => { const [,...wine] = input; if(wine.length < 2) { console.log(wine[0]); process.exit(); } else if (wine.length < 2) { console.log(wine[0] + wine[1]); process.exit(); } const memo = .. 더보기
[node.js] 백준 2579번 계단 오르기 const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const input = []; rl.on('line', (line) => { input.push(Number(line)); }) .on('close', () => { const [n, ...step] = input; const memo = {}; memo[0] = step[0]; memo[1] = Math.max(step[0] + step[1], step[1]); memo[2] = Math.max(step[0] + step[2], step[1] + step[2]); for(let i.. 더보기

728x90