memostack
article thumbnail
백준 1316번 / 그룹 단어 체커 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 9. 1. 22:06

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { const ls = input.slice(1, +input[0] + 1); return ls.reduce((cnt, l) => { const visit = {}; const cl = l.split(''); for (let i = 0; i < cl.length; i++) { const c = cl[i]; if (!visit[c]) { visit[c] = true; } else { if (c !== cl[i - 1]) { return cnt; } } } return cnt + 1; }, 0); }; const input = []; require('..

article thumbnail
백준 2908번 / 상수 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 31. 21:19

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { const nums = input.split(' ').map((s) => +s.split('').reverse().join('')); return Math.max(...nums); }; require('readline') .createInterface(process.stdin, process.stdout) .on('line', (input) => console.log(solution(input))) .on('close', () => process.exit(0)); 상세 풀이 배열에 reverse() 함수를 사용하면, 배열의 원소를 역순으로 재정..

article thumbnail
백준 1152번 / 단어의 개수 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 30. 21:16

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => input.split(' ').filter((s) => !!s).length; require('readline') .createInterface(process.stdin, process.stdout) .on('line', (input) => { console.log(solution(input)); }) .on('close', () => { process.exit(0); }); 상세 풀이 단어를 구분하는 기준은 띄어쓰기 시작하는 부분부터 끝나는 부분이다. 즉, 빈 공백 ' '으로 split하여 개수를 카운팅하면 된다. 주의할 점으로는 문장의 맨 앞..

article thumbnail
백준 2675번 / 문자열 반복 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 29. 20:55

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { return input .slice(1, input.length) .reduce((acc, l) => { const [n, str] = l.split(' '); acc.push(str.split('').reduce((acc, c) => acc + c.repeat(+n), '')); return acc; }, []) .join('\n'); }; const input = []; require('readline') .createInterface({ input: process.stdin }) .on('line', (line) => input.push(..

article thumbnail
백준 2941번 / 크로아티아 알파벳 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 25. 21:32

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { const s = input.split(''); let cnt = 0; for (let i = 0; i < s.length; i++) { const c = s[i]; const nc = s[i + 1]; const nnc = s[i + 2]; if (c === 'd') { if (i + 2 < s.length && nc === 'z' && nnc === '=') { i += 2; } else if (i + 1 < s.length && nc === '-') { i++; } } else if (c === 'c' && i + 1 < s.length ..

article thumbnail
백준 5622번 / 다이얼 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 24. 21:25

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { const dial = [...Array(26).keys()].reduce((acc, i) => { const n = 65 + i; const c = String.fromCharCode(n); if (n < 68) acc[c] = 2; else if (n < 71) acc[c] = 3; else if (n < 74) acc[c] = 4; else if (n < 77) acc[c] = 5; else if (n < 80) acc[c] = 6; else if (n < 84) acc[c] = 7; else if (n < 87) acc[c] = 8; e..

article thumbnail
백준 1157번 / 단어 공부 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 21. 12:04

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 전체 코드 const solution = (input) => { const en = new Array(26).fill(0); let max = Number.MIN_SAFE_INTEGER; input .toUpperCase() .split('') .forEach((c) => { const idx = c.charCodeAt() - 65; en[idx]++; max = max > en[idx] ? max : en[idx]; }); if (en.filter((n) => n === max).length > 1) return '?'; for (let i = 0; i < en.length; i++) { if (en[i]..

article thumbnail
백준 1065번 / 한수 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 16. 21:48

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 const isHanNum = (n) => { if (n 0) { const curNum = t % 10; if (prevNum != null) { const curDelta = prevNum - curNum; if (delta != null && curDelta !== delta) { return false; } delta = curDelta; } prevNum = curNum; t = parseInt(t / 10); } return true; }; const solution = ..

article thumbnail
백준 4673번 / 셀프 넘버 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 15. 21:06

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 const d = (n) => { const answer = [n]; let t = n; while (t > 0) { answer.push(t % 10); t = parseInt(t / 10); } return answer.reduce((acc, n) => acc + n, 0); }; const solution = () => { const checkSelfNum = [...Array(10001).keys()].map((_) => true); [...Array(10000).keys()].forEach((i) => (checkSelfNum[d(i + 1)] = false)); return checkSelfNum..

article thumbnail
백준 4344번 / 평균은 넘겠지 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 14. 13:17

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 반 평균을 먼저 구한뒤에 각 학생의 점수를 하나씩 비교하여, 평균보다 크면 카운트를 증가시켜주도록 했다. const solution = (input) => { return [...Array(+input[0]).keys()] .map((idx) => { const list = input[idx + 1].split(' ').map((c) => +c); const studentCount = list[0]; const scores = list.slice(1, list.length); const avg = scores.reduce((acc, n) => acc + n, 0) / studentCount; return `..

article thumbnail
백준 3052번 / 나머지 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 11. 21:19

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 중복 제거를 위해 집합의 성질을 활용하면 쉽게 풀 수 있다. const solution = (input) => new Set(input.map((s) => +s % 42)).size; const input = []; require('readline') .createInterface({ input: process.stdin }) .on('line', (line) => input.push(line)) .on('close', (_) => { console.log(solution(input)); process.exit(0); }); 전체 코드 https://github.com/bluemiv/Algorithm/blo..

article thumbnail
백준 2577번 / 숫자의 개수 (nodejs 알고리즘 풀이)
Algorithm/Beakjoon 2022. 8. 10. 21:10

문제의 저작권은 백준 알고리즘(https://www.acmicpc.net/)에 있습니다 문제 풀이 입력받은 숫자의 곱을 구하고, 각 숫자가 몇개씩 들어갔는지 출력하는 문제이다. 대부분 곱을 구한뒤에 문자열로 치환해서 이터레이션 돌면서 값을 구할거 같다는 생각이 들어서(아님 말고), 본 글에서는 나눗셈과 나머지 연산을 사용하여 문제를 풀었다. const solution = (input) => { let rs = input.reduce((acc, s) => acc * +s, 1); const numTable = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; while (rs > 0) { numTable[rs % 10]++; rs = parseInt(rs / 10); } return numTable..

article thumbnail
기존 React App에 Typescript 적용하기
Frontend/React 2022. 3. 2. 20:14

기존 React App에 Typescript 적용하기 Typescript 의존성 추가 타입 스크립트를 적용하기 위해 필요한 라이브러리들을 package.json에 의존성을 추가한다. (아래 명령어 참고) $ yarn add typescript @types/node @types/react @types/react-dom @types/jest --dev 위 명령어를 수행하면 아래와 같이 package.json > devDependencies 에 의존성 라이브러리들이 추가된다. { ... "devDependencies": { "@types/jest": "^27.4.1", "@types/node": "^17.0.21", "@types/react": "^17.0.39", "@types/react-dom": "^17..

Javascript 연, 월, 일 계산하기 (날짜 조작)
Frontend/HTML, CSS, JS 2020. 7. 3. 21:31

Javascript 에서 날짜 조작하기 현재 시간 가져오기 // 오늘 const today = new Date(); console.log(today.toLocaleString()); // 특정 날짜 const date = new Date(2020, 7, 2); console.log(date.toLocaleString()); 연도 더하기/빼기 setFullYear()와 getFullYear()를 활용 // 내년 const nextYear = new Date(2020, 7, 2); nextYear.setFullYear(nextMonth.getFullYear() + 1); console.log(nextYear.toLocaleString()); 월 더하기/빼기 setMonth()와 getMonth()를 활용 /..