문제https://leetcode.com/problems/generate-parentheses/?envType=problem-list-v2&envId=dynamic-programming풀이해당 문제는 백트레킹 알고리즘을 적용해 풀 수 있다.sol 함수에서 여는 괄호의 갯수가 n보다 작다면 여는 괄호를 추가한 문자열을 재귀 동작으로 함수를 호출하고, 다른 if문에서는 여는 괄호의 갯수가 닫는 괄호의 갯수보다 많을 경우 닫는 경우의 조합을 구하기 위해 닫는 괄호를 추가한 문자열을 함수 호출해준다.코드/** * @param {number} n * @return {string[]} */const generateParenthesis = (n) => { const result = []; const sol..
코딩 테스트
문제https://leetcode.com/problems/sort-colors/?envType=daily-question&envId=2025-05-17 풀이숫자 배열의 정렬 문제로써, 0은 항상 앞에, 2는 항상 뒤에 있어야 한다. 그렇다면 포인터를 이용해 값에 대한 검증을 진행하고 Swap 동작으로 각 값의 위치를 변경해주며 값을 정렬해줄 수 있다.(i는 현재 값의 포인터, j는 0이 들어갈 위치 포인터, k는 2가 들어갈 위치 포인터) 코드/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */const sortColors = (nums) => { let [i, j, k..
문제https://www.acmicpc.net/problem/2668 풀이이 문제는 DFS알고리즘을 이용해 각 숫자의 사이클 여부를 확인해 답을 도출하는 문제이다. DFS 알고리즘에 사이클 시작 숫자와 현재의 숫자를 전달을 하고 해당 숫자의 값이 방문되었으며 시작 값과 같다면 정답 집합에 속하는 숫자이므로 답을 도출할 수 있다. 코드const input = require("fs") .readFileSync( process.platform === "linux" ? "/dev/stdin" : __dirname + "/example.txt" ) .toString() .trim() .split("\n") .map((e) => e.trim());const [n, ...number] = input...
문제https://www.acmicpc.net/problem/7682 풀이해당 문제에서 실수한 부분을 정리해보겠다.가로 세로에서 일자가 완성되는 경우를 측정할때, 인덱싱에서의 실수각 일자(가로 세로 대각선)을 측정할 때 "."인지 검증 하지 않은 실수일자가 하나도 안나왔을 경우 "."의 남은 개수를 고려하지 않고 모두 valid 처리한 실수위의 요소들만 주의하면 나머지 조건은 쉽게 풀린다.코드const input = require("fs") .readFileSync( process.platform === "linux" ? "/dev/stdin" : __dirname + "/example.txt" ) .toString() .trim() .split("\n") .map((e) => e.tri..
문제https://www.acmicpc.net/problem/15989 코드const input = require("fs") .readFileSync( process.platform === "linux" ? "/dev/stdin" : __dirname + "/example.txt" ) .toString() .trim() .split("\n") .map((e) => e.trim());const n = +input.shift();const arr = input.map(Number);const max = Math.max(...arr);const dp = Array.from({ length: max + 1 }, () => 1);let answer = "";for (let i = 2; i
문제https://www.acmicpc.net/problem/14940 코드const input = require("fs") .readFileSync( process.platform === "linux" ? "/dev/stdin" : __dirname + "/example.txt" ) .toString() .trim() .split("\n") .map((e) => e.trim());const [n, m] = input.shift().split(' ').map(Number);const map = input.map(e => e.split(' ').map(Number));const result = Array.from({ length: n }, () => Array.from({ length: m..