본문 바로가기

728x90

Algorithm/programmers

[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; }; 더보기
[NodeJS] 프로그래머스 폰켓몬 const solution = (nums) => { const cnt = nums.length / 2; const setLen = [...new Set(nums)].length; return Math.min(cnt, setLen); }; 더보기
[NodeJS] 프로그래머스 위장 /** * @link https://school.programmers.co.kr/learn/courses/30/lessons/42578?language=javascript */ const solution = (clothes) => { let answer = -1; const map = {}; for (let i = 0; i pre * cur); }; 더보기
[NodeJS] 프로그래머스 베스트앨범 /** * @link https://school.programmers.co.kr/learn/courses/30/lessons/42579?language=javascript * @description */ const solution = (genres, plays) => { if (plays.length === 1) { return [0]; } const answer = []; const map = {}; const indexMap = {}; const countArr = [...new Set(genres)].map((v, i) => { indexMap[v] = i; return [v, 0]; }); if (countArr.length === 1) { const arr = []; for (let i = 0;.. 더보기

728x90