Algorithm/BOJ

Algorithm/BOJ

[알고리즘/백준/9012] 괄호(스택)

문제 https://www.acmicpc.net/problem/9012 9012번: 괄호 괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고 www.acmicpc.net 풀이 const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; let [n, ...arr] = fs.readFileSync(filePath).toString().trim().split("\n"); const N = +n; const p..

Algorithm/BOJ

[알고리즘/백준/14940] 쉬운 최단거리(BFS, Nodejs)

문제 https://www.acmicpc.net/problem/14940 14940번: 쉬운 최단거리 지도의 크기 n과 m이 주어진다. n은 세로의 크기, m은 가로의 크기다.(2 ≤ n ≤ 1000, 2 ≤ m ≤ 1000) 다음 n개의 줄에 m개의 숫자가 주어진다. 0은 갈 수 없는 땅이고 1은 갈 수 있는 땅, 2는 목표지점이 www.acmicpc.net 풀이 class Queue { constructor() { this.data = []; this.head = 0; this.tail = 0; } push(item) { this.data[this.tail++] = item; } pop() { this.head++; } front() { return this.data[this.head]; } rear..

Algorithm/BOJ

[알고리즘/백준/10828] 스택(자료구조)

문제 https://www.acmicpc.net/problem/10828 10828번: 스택 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 www.acmicpc.net 풀이 class Stack { constructor() { this.stack = []; } push(newElement) { this.stack.push(newElement); } pop() { if (this.empty()) return -1; return this.stack.pop(); } size() { return this.stack.length; } empty()..

Algorithm/BOJ

[알고리즘/백준/16918] 봄버맨(BFS, Nodejs)

문제 https://www.acmicpc.net/problem/16918 16918번: 봄버맨 첫째 줄에 R, C, N (1 ≤ R, C, N ≤ 200)이 주어진다. 둘째 줄부터 R개의 줄에 격자판의 초기 상태가 주어진다. 빈 칸은 '.'로, 폭탄은 'O'로 주어진다. www.acmicpc.net 풀이 const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; const [N, ...arr] = fs.readFileSync(filePath).toString().trim().split("\n"); const [r, c, n] = N.split(" ").map(Number); co..

Algorithm/BOJ

[알고리즘/백준/2667] 단지 번호 붙이기

문제 https://www.acmicpc.net/problem/2667 2667번: 단지번호붙이기 과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여 www.acmicpc.net 풀이 class Queue { constructor() { this.data = []; this.head = 0; this.tail = 0; } push(item) { this.data[this.tail++] = item; } pop() { this.head++; } front() { return this.data[this.head]; } rear() { return this.data[this..

Algorithm/BOJ

[알고리즘/백준/1325] 효율적인 해킹(BFS, JavaScript, 백준 Nodejs로 풀 때 유의사항)

문제 https://www.acmicpc.net/problem/1325 row.split(" ").map(Number)); let graph = Array.from({ length: n + 1 }, () => []); let hacked = Array.from({ length: n + 1 }, () => 0); for (let [s, e] of pairs) { graph[e].push(s); } // 각 노드에서 해킹할 수 있는 컴퓨터 수 세기 for (let i = 1; i 0); const q = new Queue(); let cnt = 1; let max = Number.MIN_SAFE_INTEGER; ch[i] = 1; q.push(i); while (!q.isEmpty()) { const v =..

Algorithm/BOJ

[알고리즘/백준/2178] 미로탐색(BFS, 최단거리)

문제 https://www.acmicpc.net/problem/2178 2178번: 미로 탐색 첫째 줄에 두 정수 N, M(2 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 M개의 정수로 미로가 주어진다. 각각의 수들은 붙어서 입력으로 주어진다. www.acmicpc.net 풀이 입력 const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; const [N, ...arr] = fs.readFileSync(filePath).toString().trim().split("\n"); const [n, m] = N.split(" ").map(Number); let board ..

Algorithm/BOJ

[알고리즘/백준/11725] 트리의 부모 찾기(DFS, BFS, 백준 console 시간 초과 해결)

문제 https://www.acmicpc.net/problem/11725 11725번: 트리의 부모 찾기 루트 없는 트리가 주어진다. 이때, 트리의 루트를 1이라고 정했을 때, 각 노드의 부모를 구하는 프로그램을 작성하시오. www.acmicpc.net 풀이 그래프를 입력 받아 인접 리스트를 만들고, 해당 리스트를 DFS와 BFS로 순회하면서 [부모, 자식]을 배열에 저장한다. 자식을 기준으로 sort한 다음 부모만 map으로 추출하여 answer를 구한다. for문에서 배열을 순회하며 console.log()를 했더니 시간 초과가 났다. 백준에서는 conosole로 답을 출력할 때 한 번에 모아서 처리해야 한다는 점에 유의해야겠다. 또 #1, #2처럼 배열에 따로 [부모, 자식]을 저장해서 sort하는..

개발자 김비숑
'Algorithm/BOJ' 카테고리의 글 목록 (5 Page)