본문 바로가기

728x90

프로그래머스

[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 더보기
[NodeJS] 프로그래머스 다단계 칫솔 판매 const solution = (enroll, referral, seller, amount) => { const idxMap = {}; enroll = enroll.map((name, idx) => { idxMap[name] = idx; return new Node(name); }); referral.forEach((name, idx) => { if (name === '-') return; const parentIdx = idxMap[name]; const parent = enroll[parentIdx]; const me = enroll[idx]; parent.addChild(me); me.setParent(parent); }); for (let i = 0; i < seller.length; i++) {.. 더보기
[NodeJS] 프로그래머스 N으로 표현 const solution = (n, number) => { if (n === number) { return 1; } const memo = [...new Array(9)].map((v, idx) => idx === 0 ? new Set() : new Set([Number(String(n).repeat(idx))]), ); const calc = (i) => { for (let j = 1; j < i; j++) { for (const n1 of memo[j]) { for (const n2 of memo[i - j]) { const plus = n1 + n2; const minus = n1 - n2; const multiplied = n1 * n2; const divided = Math.floor(n1 /.. 더보기
[NodeJS] 프로그래머스 수식 최대화 const solution = (expression) => { let answer = 0; const recursive = (ops, nums, orders) => { if (!orders.length) { answer = Math.max(answer, Math.abs(nums[0])); return; } const [o, ...newOrders] = orders; let idx = ops.findIndex((v) => v === o); while (idx > -1) { ops.splice(idx, 1); let temp; switch (o) { case '+': temp = nums[idx] + nums[idx + 1]; break; case '-': temp = nums[idx] - nums[idx .. 더보기
[NodeJS] 프로그래머스 빛의 경로 사이클 const solution = (grid) => { const answer = []; const m = grid.length; const n = grid[0].length; const visited = []; for (let i = 0; i { let to = from; switch (grid[x][y]) { case 'S': if (to === 0) { x--; } else if (to === 1) { x++; } else if (to === 2) { y--;.. 더보기

728x90