본문 바로가기

728x90

Algorithm/programmers

[NodeJS] 프로그래머스 피보나치 수 const solution = (n) => { const memo = [0, 1]; if (n < memo) { return memo[n]; } for (let i = memo.length; i 더보기
[NodeJS] 프로그래머스 최댓값과 최솟값 const solution = (s) => { let max = Number.MIN_SAFE_INTEGER; let min = Number.MAX_SAFE_INTEGER; for (const c of s.split(' ').map(Number)) { max = Math.max(c, max); min = Math.min(c, min); } return `${min} ${max}`; }; 더보기
[NodeJS] 프로그래머스 주차 요금 계산 const solution = (fees, records) => { const answer = []; const map = {}; const [basicMinutes, basicCost, unitTime, unitCost] = fees; const getMinutes = (hour, minutes) => hour * 60 + minutes; const getCharge = (time) => { let charge = basicCost; time -= basicMinutes; if (0 < time) { charge += Math.ceil(time / unitTime) * unitCost; } return charge; }; for (const record of records) { const [strTim.. 더보기
[NodeJS] 프로그래머스 숫자의 표현 function solution(n) { let answer = n % 2 === 1 ? 2 : 1; let sum = 0; let max = 1; let min = 1; if (n === 1) { return 1; } while (max 더보기
[NodeJS] 프로그래머스 이진 변환 반복하기 const convertBinaryString = (num) => { const arr = []; while (0 { let removeZeroCnt = 0; let tryCnt = 0; while (s !== '1') { tryCnt++; const prevLen = s.length; const oneCnt = [...s].filter((v) => v === '1').length; removeZeroCnt += prevLen - oneCnt; s = convertBinarySt.. 더보기
[NodeJS] 프로그래머스 전력망을 둘로 나누기 const solution = (n, wires) => { let answer = 999; const map = {}; const getCnt = (node, exceptionNode) => { let cnt = 0; const queue = [node]; const set = new Set(); set.add(node); set.add(exceptionNode); while (queue.length) { const curNode = queue.pop(); cnt++; for (const n of map[curNode]) { if (!set.has(n)) { set.add(n); queue.push(n); } } } return cnt; }; for (const w of wires) { const [v1.. 더보기
[NodeJS] 프로그래머스 양궁대회 const solution = (n, info) => { const infoLen = info.length; let answer = new Array(infoLen).fill(0); let diff = 0; const recursive = (num, idx, arr) => { if (num === 0) { // 화살을 다 소모 했을경우 let apeach = 0; // 어피치 점수 let ryan = 0; // 라이언 점수 // 점수 계산 for (let i = 0; i < infoLen; i++) { const ap = info[i]; const ry = arr[i]; if (ap < ry) { ryan += 10 - i; } else if (ry < ap) { apeach += 10 - i; } } .. 더보기
[NodeJS] 프로그래머스 입국 심사 const solution = (n, times) => { // 이분탐색을 위해 오름차순 정렬 times.sort((a, b) => a - b); let left = 1; let right = n * times[times.length - 1]; let answer = right; // 최댓값 while (left { // 한 사람당 몇명 할 수 있는지 cnt += Math.floor(mid / v); if (n 더보기

728x90