[백준 | 실버 1 | JavaScript] 2667번, 단지 번호 붙이기
·
알고리즘/문제
Link2667번 - 단지 번호 붙이기 문제풀이(1) DFS 재귀let input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");let idx = 0;const N = Number(input[idx++]);const map = Array.from({ length: N }, () => []);const visited = Array.from({ length: N }, () => Array.from({ length: N }, () => false));const answers = [];const dr = [0, 1, 0, -1];const dc = [1, 0, -1, 0];for (let n = 0; n { visited[..
[백준 | 실버 1 | JavaScript] 2468번, 안전 영역
·
알고리즘/문제
Link2468번 - 안전 영역 문제풀이(1) DFS 재귀 구현let input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");let idx = 0;const N = Number(input[idx++]);const map = [];let maxAnswer = 0;const dn = [0, 1, 0, -1];const dm = [1, 0, -1, 0];for (let n = 0; n { return n = N || m >= N;};const dfsRecursive = (n, m, visited, value) => { visited[n][m] = true; for (let d = 0; d Array.from({..
[백준 | 실버 2 | JavaScript] 11724번, 연결 요소의 개수
·
알고리즘/문제
Link11724번 - 연결 요소의 개수 문제풀이(1) 재귀 구현let input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");let idx = 0;const [N, M] = input[idx++].split(" ").map(Number);const graph = Array.from({ length: N + 1 }, () => []);const visited = Array.from({ length: N + 1 }, () => false);let answer = 0;for (let m = 0; m { visited[node] = true; for (let g of graph[node]) { if (visited..
[백준 | 실버 2 | JavaScript] 1012번, 유기농 배추
·
알고리즘/문제
Link1012번 - 유기농 배추 문제풀이(1) DFS 재귀let input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");let idx = 0;const T = Number(input[idx++]);const dn = [0, 1, 0, -1];const dm = [1, 0, -1, 0];const answer = Array.from({ length: T }, () => 0);for (let t = 0; t Array.from({ length: M }, () => false) ); const visited = Array.from({ length: N }, () => Array.from({ length: M..
[백준 | 실버 3 | JavaScript] 2606번, 바이러스
·
알고리즘/문제
Link2606번 - 바이러스 문제풀이(1) 재귀 DFSvar input = require("fs") .readFileSync("/dev/stdin") .toString() .trim() .split("\n");const N = Number(input[0]);const graph = Array.from({ length: N + 1 }, () => []);for (let i = 2; i (2) 스택 DFSvar input = require("fs") .readFileSync("/dev/stdin") .toString() .trim() .split("\n");const N = Number(input[0]);const graph = Array.from({ length: N + 1 }, () =>..
[백준 | 실버 2 | JavaScript] 11725번, 트리의 부모 찾기
·
알고리즘/문제
Link11725번 - 트리의 부모 찾기 문제풀이(1)var input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");const N = Number(input[0]);let graph = Array.from({ length: N + 1 }, () => []);// 트리의 간선은 N-1개이므로 해당 줄만 처리for (let i = 1; i (2)var input = require("fs").readFileSync("/dev/stdin").toString().trim().split("\n");const N = Number(input[0]);let graph = Array.from({ length: N + 1 }, () => ..
[프로그래머스 | Lv. 1 | JavaScript] 햄버거 만들기
·
알고리즘/문제
Link코딩테스트 연습 - 햄버거 만들기 문제풀이(1)function solution(ingredient) { const stack = []; let hamburgerCount = 0; for (let ing of ingredient) { stack.push(ing); // stack의 마지막 4개 재료가 [1, 2, 3, 1] 인지 확인 if ( stack.length >= 4 && stack[stack.length - 4] === 1 && stack[stack.length - 3] === 2 && stack[stack.length - 2] === 3 && stack[stack.length - 1] === 1 ) { ..
[프로그래머스 | Lv. 1 | JavaScript] 숫자 짝꿍
·
알고리즘/문제
Link코딩테스트 연습 - 숫자 짝꿍 문제풀이(1)function solution(X, Y) { const countX = new Array(10).fill(0); const countY = new Array(10).fill(0); for (const ch of X) { countX[parseInt(ch)]++; } for (const ch of Y) { countY[parseInt(ch)]++; } let result = ""; for (let digit = 9; digit >= 0; digit--) { const commonCount = Math.min(countX[digit], countY[digit]);..
[프로그래머스 | Lv. 1 | JavaScript] 체육복
·
알고리즘/문제
Link코딩테스트 연습 - 체육복 문제풀이(1)function solution(n, lost, reserve) { let answer = 0; const realLost = lost.filter(student => !reserve.includes(student)); const realReserve = reserve.filter(student => !lost.includes(student)); realLost.sort((a, b) => a - b); let set = new Set(realReserve); realLost.forEach(item => { if (item - 1 >= 1 && set.has(item - 1)) { ..
[프로그래머스 | Lv. 1 | JavaScript] 완주하지 못한 선수
·
알고리즘/문제
Link코딩테스트 연습 - 완주하지 못한 선수 문제풀이(1)function solution(participant, completion) { let map = {}; let p; for (let i = 0; i (2)function solution(participant, completion) { const count = {}; // 참가자 배열에서 각 이름의 등장 횟수를 기록합니다. participant.forEach(name => { count[name] = (count[name] || 0) + 1; }); // 완주자 배열에서 각 이름의 카운트를 감소시킵니다. completion.forEach(name => { count[name]--; }); // ..