본문 바로가기

728x90

Algorithm

[NodeJS] 프로그래머스 메뉴 리뉴얼 const solution = (orders, course) => { const answer = []; const recursive = (obj, order, cnt, idx, prev) => { if (prev.length === cnt) { const str = prev.sort().join(''); obj[str] = obj[str] ? [str, obj[str][1] + 1] : [str, 1]; } for (let i = idx; i { const menu = {}; orders.forEach((o) => {.. 더보기
[NodeJS] 프로그래머스 두 큐 합 같게 만들기 const solution = (q1, q2) => { const arr = [...q1, ...q2]; const loopCnt = arr.length * 4; const getSum = (arr) => arr.reduce((pre, cur) => pre + cur); let sum = getSum(arr); if (sum % 2 !== 0) { return -1; } const v = sum / 2; let calcSum = getSum(q1); let cnt = 0; let s = 0; let e = q1.length; while (cnt < loopCnt) { if (calcSum === v) { return cnt; } else if (v < calcSum) { calcSum -= arr[s];.. 더보기
[NodeJS] 프로그래머스 H-index const solution = (citations) => { const len = citations.length; if (len === 1) { return citations[0] === 1 ? 1 : 0; } let answer = 0; citations.sort((a, b) => b - a); while (answer + 1 더보기
[NodeJS] 프로그래머스 같은 숫자는 싫어 const solution = (arr) => { const answer = []; for (const n of arr) { if (answer[answer.length - 1] !== n) { answer.push(n); } } return answer; }; 더보기
[NodeJS] 프로그래머스 올바른 괄호 const solution = (str) => { if (str[0] === ')') { return false; } const stack = [str[0]]; for (let i = 1; i 0) { stack.pop(); } else { return false; } } } return stack.length > 0 ? false : true; }; 더보기
[NodeJS] 프로그래머스 프린터 const solution = (priorities, location) => { let answer = 0; const v = `${location}-${priorities[location]}`; priorities = priorities.map((v, i) => [i, v]); let cnt = 0; while (priorities.length) { const cur = priorities.shift(); let is = true; for (let i = 0; i < priorities.length; i++) { const [, curN] = priorities[i]; if (cur[1] < curN) { is = false; break; } } if (!is) { priorities.push(cur).. 더보기
[NodeJS]프로그래머스 가장 큰 수 const solution = (nums) => { const strZero = '0'; nums = nums.map(String); nums.sort((a, b) => b + a - (a + b)); return nums[0] === strZero ? strZero : nums.join(''); }; 더보기
[NodeJS] 프로그래머스 K번째 수 const solution = (array, commands) => { const answer = []; for (const [i, j, k] of commands) { const arr = array.slice(i - 1, j).sort((a, b) => a - b); answer.push(arr[k - 1]); } return answer; }; 더보기

728x90