본문 바로가기

코딩테스트 문제풀이

(16)
[프로그래머스, javascript] 이중우선순위큐 function solution(operations) { var answer = []; function Queue(){ this.q = []; this.insert = function(num){ this.q.push(num); }; this.deleteElement = function(idx){ this.q.splice(idx, 1); } this.deleteMax = function(){ if(this.q.length > 0){ const MAX = Math.max(...this.q); const INDEX = this.q.indexOf(MAX); this.deleteElement(INDEX); } }; this.deleteMin = function(){ if(this.q.length > 0){ con..
[프로그래머스, javascript] 네트워크 function solution(n, computers) { var visited = new Array(n).fill(false); function visit(q, arr){ if(!q.length) return; const val = q.pop(); visited[val] = true; arr[val].map((v,idx)=>{ if(!!v && !q.includes(idx) && !visited[idx]){ q.push(idx) } }) return visit(q, arr); } var network = 0; visited.map((v,i)=>{ if(!v){ //방문 안했을 경우? network++; visit([i], computers); }; }); return network; } program..
[구름, javascript] 배열 합치기 const fs = require('fs'); const stdin = (process.platform === 'linux' ? fs.readFileSync('/dev/stdin').toString() : `5 4 2 3 6 7 8 1 8 9 11`).split('\n'); const input = (() => { let line = 0; return () => stdin[line++]; })(); function solution(A, B){ let arrA = input().split(' ').map(v=>+v); let arrB = input().split(' ').map(v=>+v); console.log((arrA.concat(arrB)).sort((a, b) => a-b).join(' ')+' ..
[프로그래머스, javascript] 타겟 넘버 DFS와 BFS 풀이 문제 설명 n개의 음이 아닌 정수가 있습니다. 이 수를 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다. -1+1+1+1+1 = 3 +1-1+1+1+1 = 3 +1+1-1+1+1 = 3 +1+1+1-1+1 = 3 +1+1+1+1-1 = 3 사용할 수 있는 숫자가 담긴 배열 numbers, 타겟 넘버 target이 매개변수로 주어질 때 숫자를 적절히 더하고 빼서 타겟 넘버를 만드는 방법의 수를 return 하도록 solution 함수를 작성해주세요. 제한사항 주어지는 숫자의 개수는 2개 이상 20개 이하입니다. 각 숫자는 1 이상 50 이하인 자연수입니다. 타겟 넘버는 1 이상 1000 이하인 자연수입니다. ..
[프로그래머스, javascript] 가장 긴 팰린드롬 function isPalin(s){ let length = parseInt(s.length / 2) for(let i=0; i= 0; i--){ for(let j=0; j
[프로그래머스, javascript] 여행 경로 function solution(tickets) { var routes = [] fna([], tickets); function fna(route, left){ if(left.length == 0){ let tmp = ['ICN'] route.map(v => {tmp.push(v[1])}) routes.push(tmp) return } if(route.length > 0 && route[0][0] !== 'ICN') return if(route.length >= 2&& route[route.length-2][1] !== route[route.length-1][0]) return left.map((v, i) =>{ fna([...route, v], left.filter((v,idx) => i!=idx)) ..
[프로그래머스, javascript]쿼드 압축 후 개수 세기 function fnA(arr, lx, ly, rx, ry, t){ let el = arr[ly][lx] for(let i=ly; i
[백준 16235] 나무 재테크 #include #include #include #include #include using namespace std; class Field { private: vector age;//size는 나무 개수 int feed; int incFeed; int deadLine; public: Field() { feed = 5; } void plant(int _age) { age.push_back(_age); } //나무를 형성하는 함수 void setIncFeed(int _incFeed) { incFeed = _incFeed; } //양분 증가량 설정 void ageSort() { sort(age.begin(), age.end()); } //오름차순 정렬 void feedEat() { deadLine = -1; ..