문제
1 0커플


2 폴더 폰 자판


3 구름이의 여행



4 순환하는 수로




풀이
효성
1
const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const input = [] rl.on('line', (line) => { input.push(line.trim()) if (input.length === 2) { rl.close() } }) .on('close', () => { solution(input) process.exit(); }) })(); const solution = (data) => { const [n, string] = data; const scoreArr = string.split(' '); let answer = 0; scoreArr.forEach(score => { answer += Number(score); }); console.log(answer); }
2
const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const input = [] rl.on('line', (line) => { input.push(line.trim()) if (input.length === 2) { rl.close() } }) .on('close', () => { solution(input) process.exit(); }) })(); const solution = (data) => { const [n, s] = data; const sArr = s.split(''); const board = [ [], ['1', '.', ',', '?', '!'], ['2', 'A', 'B', 'C'], ['3', 'D', 'E', 'F'], ['4', 'G', 'H', 'I'], ['5', 'J', 'K', 'L'], ['6', 'M', 'N', 'O'], ['7', 'P', 'Q', 'R', 'S'], ['8', 'T', 'U', 'V'], ['9', 'W', 'X', 'Y', 'Z'] ]; let curChar = sArr[0]; let charCnt = 0; let answer = ''; const addTargetToAnswer = (char, cnt) => { cnt -= 1; const len = board[Number(char)].length; if(len <= cnt) { cnt %= len; } const target = board[Number(char)][cnt]; answer += target; } sArr.forEach(pattern => { if(curChar === pattern) { charCnt += 1; } else { addTargetToAnswer(curChar, charCnt); curChar = pattern; charCnt = 1; } }); addTargetToAnswer(curChar, charCnt); console.log(answer); }
3
const readline = require('readline'); (async () => { const rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', function (line) { inputs.push( line .trim() .split(' ') .map((v) => +v) ); }).on('close', function () { main(inputs); process.exit(); }); })(); class MyQueue { constructor() { this.input = []; this.output = []; this.size = 0; } enque(val) { this.input.push(val); this.size++; } deque() { if (this.output.length === 0) { this.output = this.input.reverse(); this.input = []; } this.size--; return this.output.pop(); } } function main(inputs) { const graph = new Map(); const [n, m, k] = inputs.shift(); const makeGraph = (key, value) => { const prevValue = graph.get(key); if(prevValue) { prevValue.push(value); } else { graph.set(key, [value]); } } inputs.forEach(input => { const [u, v] = input; makeGraph(u, v); makeGraph(v, u); }); const queue = new MyQueue(); let minCnt = Infinity; let isVisited = Array(n + 1).fill(false); isVisited[1] = true; queue.enque([1, 0]); while(queue.size) { const [island, cnt] = queue.deque(); if(island == n && cnt < minCnt) { minCnt = cnt; continue; } const islandArrFromIsland = graph.get(island); if(!Array.isArray(islandArrFromIsland)) { continue; } for(const nextIsland of islandArrFromIsland) { if(isVisited[nextIsland]) { continue; } isVisited[nextIsland] = true; queue.enque([nextIsland, cnt + 1]); } } minCnt <= k ? console.log('YES') : console.log('NO'); }
4
재영
1
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())).on('close', () => { main(inputs); process.exit(); }); })(); function main(arr) { const [n, scoresString] = arr; const scores = scoresString.split(' '); const scoreMap = new Map(); for (let i = 0; i < n; i += 1) { const nowScore = +scores[i]; const partnerScore = -nowScore; if (scoreMap.has(partnerScore)) { scoreMap.delete(partnerScore); } else { scoreMap.set( nowScore, scoreMap.has(nowScore) ? scoreMap.get(nowScore) : 0 + 1 ); } } const result = [...scoreMap].reduce( (acc, [key, value]) => acc + key * value, 0 ); console.log(result); }
2
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line)).on('close', () => { main(inputs); process.exit(); }); })(); function main(inputs) { const phoneKeyboard = [ null, '1.,?!', '2ABC', '3DEF', '4GHI', '5JKL', '6MNO', '7PQRS', '8TUV', '9WXYZ', ]; let result = ''; const [n, numbers] = inputs; // 1111 222 33 let prev = ''; let cnt = 0; for (let i = 0; i < n; i += 1) { const now = numbers[i]; if (now === prev) { cnt += 1; } else { if (prev) { const phoneKey = phoneKeyboard[prev]; result += phoneKey[(cnt - 1) % phoneKey.length]; } cnt = 1; prev = now; } } console.log( result + (prev ? phoneKeyboard[prev][(cnt - 1) % phoneKeyboard[prev].length] : '') ); }
3
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())).on('close', () => { main(inputs); process.exit(); }); })(); class Queue { constructor() { this.arr = []; this.front = 0; this.rear = 0; } enqueue(value) { this.arr.push(value); this.rear += 1; } dequeue() { if (!this.length) return; const value = this.arr[this.front]; delete this.arr[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } function main(arr) { const visited = new Array(5001).fill(false); const [baseInfo, ...graphInfos] = arr; /* eslint-disable-next-line */ const [n, m, k] = baseInfo.split(' '); const queue = new Queue(); const graph = {}; graphInfos.forEach((val) => { const [from, to] = val.split(' '); graph[from] = graph[from] ? [...graph[from], to] : [to]; graph[to] = graph[to] ? [...graph[to], from] : [from]; }); visited[1] = true; graph[1].forEach((to) => { queue.enqueue([1, to]); // [cnt(몇 번 걸쳤는지), to(목적지)] }); let flag = false; // return flag while (queue.length) { const [cnt, to] = queue.dequeue(); if (cnt > k) break; if (visited[to]) continue; if (to === n) { flag = true; break; } visited[to] = true; graph[to].forEach((next) => queue.enqueue([cnt + 1, next])); } console.log(flag ? 'YES' : 'NO'); }
4
지긋지긋한 런타임 에러… 진짜 싫네용…
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())) .on('close', () => { main(inputs); process.exit(); }); })(); class Queue { constructor() { this.arr = []; this.front = 0; this.rear = 0; } enqueue(value) { this.arr.push(value); this.rear += 1; } dequeue() { const value = this.arr[this.front]; delete this.arr[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } function main(arr) { const [n, ...graphInfos] = arr; const pushNotCycle = (queue, graph) => { for (let i = 1; i < graph.length; i += 1) { if (graph[i].length === 1) { // 사이클이면 한 점이 두 점에 이어진 거 queue.enqueue(i); } } } const graph = Array.from({ length: +n + 1 }, () => []); const queue = new Queue(); graphInfos.forEach((e) => { const eArr = e.split(' '); const from = +eArr[0]; const to = +eArr[1]; graph[from].push(to); graph[to].push(from); }) pushNotCycle(queue, graph); while (queue.length) { const now = queue.dequeue(); // 간선이 1개인 점 const next = graph[now][0]; // 목적지 if (next === undefined) continue; // 큐에 앞에 들어 있던 게 업데이트 할 경우 (이미) graph[now] = []; // 1개였으니까 뺀 건 빈배열 graph[next] = graph[next].filter(v => v !== now); // 그 친구도 업데이트 pushNotCycle(queue, graph); } const cycle = []; // 1개의 사이클이 보장되어잇으므로 결과가 나옴. for (let i = 1; i < graph.length; i += 1) { if (graph[i].length) { cycle.push(i); } } console.log(cycle.length); console.log(cycle.join(' ')) }
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())) .on('close', () => { main(inputs); process.exit(); }); })(); class Queue { constructor() { this.arr = []; this.front = 0; this.rear = 0; } enqueue(value) { this.arr.push(value); this.rear += 1; } dequeue() { const value = this.arr[this.front]; delete this.arr[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } function main(arr) { const [n, ...graphInfos] = arr; const pushNotCycle = (queue, graph) => { for (let i = 1; i < graph.length; i += 1) { if (graph[i].length === 1) { // 사이클이면 한 점이 두 점에 이어진 거 queue.enqueue(i); } } } const graph = Array.from({ length: +n + 1 }, () => []); const queue = new Queue(); graphInfos.forEach((e) => { const eArr = e.split(' '); const from = +eArr[0]; const to = +eArr[1]; graph[from].push(to); graph[to].push(from); }) pushNotCycle(queue, graph); while (queue.length) { const now = queue.dequeue(); // 간선이 1개인 점 const next = graph[now][0]; // 목적지 if (next === undefined) continue; // 큐에 앞에 들어 있던 게 업데이트 할 경우 (이미) graph[now] = []; // 1개였으니까 뺀 건 빈배열 graph[next] = graph[next].filter(v => v !== now); // 그 친구도 업데이트 pushNotCycle(queue, graph); } const cycle = []; // 1개의 사이클이 보장되어잇으므로 결과가 나옴. for (let i = 1; i < graph.length; i += 1) { if (graph[i].length) { cycle.push(i); } } console.log(cycle.length); console.log(cycle.join(' ')) }
// Run by Node.js const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); const inputs = []; rl.on('line', (line) => inputs.push(line.trim())) .on('close', () => { main(inputs); process.exit(); }); })(); class Queue { constructor() { this.arr = []; this.front = 0; this.rear = 0; } enqueue(value) { this.arr.push(value); this.rear += 1; } dequeue() { const value = this.arr[this.front]; delete this.arr[this.front]; this.front += 1; return value; } get length() { return this.rear - this.front; } } function main(arr) { const [n, ...graphInfos] = arr; const pushNotCycle = (queue, graph) => { for (let i = 1; i < graph.length; i += 1) { if (graph[i].length === 1) { // 사이클이면 한 점이 두 점에 이어진 거 queue.enqueue(i); } } } const graph = Array.from({ length: +n + 1 }, () => []); const queue = new Queue(); graphInfos.forEach((e) => { const eArr = e.split(' '); const from = +eArr[0]; const to = +eArr[1]; graph[from].push(to); graph[to].push(from); }) pushNotCycle(queue, graph); while (queue.length) { const now = queue.dequeue(); // 간선이 1개인 점 const next = graph[now][0]; // 목적지 if (next === undefined) continue; // 큐에 앞에 들어 있던 게 업데이트 할 경우 (이미) graph[now] = []; // 1개였으니까 뺀 건 빈배열 graph[next] = graph[next].filter(v => v !== now); // 그 친구도 업데이트 pushNotCycle(queue, graph); } const cycle = []; // 1개의 사이클이 보장되어잇으므로 결과가 나옴. for (let i = 1; i < graph.length; i += 1) { if (graph[i].length) { cycle.push(i); } } console.log(cycle.length); console.log(cycle.join(' ')) }
현석
// 1번 문제 const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let index = 0 let scores = null for await (const line of rl) { index++ if (index === 2) { scores = line.trim().split(' ').map(Number) } } solution(scores) process.exit(); })(); function solution(scores) { const positiveScoreMap = new Map() scores.forEach(score => { if (score > 0) positiveScoreMap.set(score, (positiveScoreMap.get(score) || 0) + 1) else positiveScoreMap.set(-score, (positiveScoreMap.get(-score) || 0) - 1) }) const totalScore = [...positiveScoreMap].reduce((acc, [key, value]) => { return acc + key * value }, 0) console.log(totalScore) }
// 2번 문제 const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let index = 0 for await (const line of rl) { index++ if (index === 2) { solution(line.trim()) rl.close(); } } process.exit(); })(); function solution(line) { const parsed = parseToArray(line).map(parseToChar).join('') console.log(parsed) } function parseToArray(sentence) { return sentence.split('').reduce((acc, curr) => { const last = acc[acc.length - 1] const latest = last[last.length - 1] if (latest) { if (latest === curr) { last.push(curr) } else { acc.push([curr]) } } else { last.push(curr) } return acc }, [[]]) }; function parseToChar(arr) { const obj = { 1: ['1', '.', ',', '?', '!'], 2: ['2', 'A', 'B', 'C'], 3: ['3', 'D', 'E', 'F'], 4: ['4', 'G', 'H', 'I'], 5: ['5', 'J', 'K', 'L'], 6: ['6', 'M', 'N', 'O'], 7: ['7', 'P', 'Q', 'R', 'S'], 8: ['8', 'T', 'U', 'V'], 9: ['9', 'W', 'X', 'Y', 'Z'] } const target = obj[arr[0]] const index = (arr.length % target.length) === 0 ? target.length - 1 : (arr.length % target.length) - 1 return target[index] }
// 3번 문제 const readline = require('readline'); (async () => { let rl = readline.createInterface({ input: process.stdin }); let index = 0 let N, M, K const bridges = [] for await (const line of rl) { if (index === 0) { [N, M, K] = line.trim().split(' ').map(Number) } else { bridges.push(line.trim().split(' ').map(Number)) } index++ } solution(N, M, K, bridges) process.exit(); })(); class PriorityQueue { constructor(sorter) { this.heap = []; this.sorter = sorter; } swap(index1, index2) { [this.heap[index1], this.heap[index2]] = [this.heap[index2], this.heap[index1]]; } getParentIndex(index) { return index !== 0 ? Math.floor((index - 1) / 2) : null; } getLeftChildIndex(index) { const leftChildIndex = 2 * index + 1; return leftChildIndex < this.heap.length ? leftChildIndex : null; } getRightChildIndex(index) { const rightChildIndex = 2 * index + 2; return rightChildIndex < this.heap.length ? rightChildIndex : null; } bubbleUp() { let index = this.heap.length - 1; let parentIndex = this.getParentIndex(index); while (parentIndex !== null && this.sorter(this.heap[index], this.heap[parentIndex])) { this.swap(index, parentIndex); index = parentIndex; parentIndex = this.getParentIndex(index); } } bubbleDown() { let index = 0; let leftChildIndex = this.getLeftChildIndex(index); let rightChildIndex = this.getRightChildIndex(index); while (true) { if (leftChildIndex === null) break; let targetIndex; if (rightChildIndex === null) { targetIndex = leftChildIndex; } else { targetIndex = this.sorter(this.heap[leftChildIndex], this.heap[rightChildIndex]) ? leftChildIndex : rightChildIndex; } if (this.sorter(this.heap[targetIndex], this.heap[index])) { this.swap(targetIndex, index); index = targetIndex; leftChildIndex = this.getLeftChildIndex(index); rightChildIndex = this.getRightChildIndex(index); } else break; } } add(data) { this.heap.push(data); this.bubbleUp(); } pop() { this.swap(0, this.heap.length - 1); const result = this.heap.pop(); this.bubbleDown(); return result; } } function solution(N, M, K, bridges) { const adjGraph = new Map() bridges.forEach(([land1,land2]) => { if (!adjGraph.has(land1)) adjGraph.set(land1, new Set()) if (!adjGraph.has(land2)) adjGraph.set(land2, new Set()) adjGraph.get(land1).add(land2) adjGraph.get(land2).add(land1) }) const dist = new Array(N + 1).fill(Infinity) dist[1] = 0 const visit = new Array(N + 1).fill(false) const minHeap = new PriorityQueue((a, b) => a[0] < b[0]) minHeap.add([0, 1]) while (minHeap.heap.length) { const [minDist, currNode] = minHeap.pop() if (visit[currNode]) continue visit[currNode] = true const nextNodes = adjGraph.get(currNode) nextNodes.forEach(next => { if (visit[next]) return dist[next] = Math.min(dist[next], dist[currNode] + 1) minHeap.add([dist[next], next]) }) } console.log(dist[N] <= K ? 'YES' : 'NO') }