diff --git a/README.md b/README.md index 2e2d8e3..c4d89fe 100644 --- a/README.md +++ b/README.md @@ -136,35 +136,7 @@ | 23 | [땅따먹기](https://school.programmers.co.kr//learn/courses/30/lessons/12913) | [땅따먹기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/땅따먹기.js) | | 24 | [멀쩡한 사각형](https://school.programmers.co.kr//learn/courses/30/lessons/62048) | [멀쩡한-사각형.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/멀쩡한-사각형.js) | | 25 | [메뉴 리뉴얼](https://school.programmers.co.kr//learn/courses/30/lessons/72411) | [메뉴-리뉴얼.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/메뉴-리뉴얼.js) | -| 26 | [모음 사전](https://school.programmers.co.kr/undefined) | [모음-사전.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/모음-사전.js) | -| 27 | [문자열 압축](https://school.programmers.co.kr//learn/courses/30/lessons/60057) | [문자열-압축.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/문자열-압축.js) | -| 28 | [빛의 경로 사이클](https://school.programmers.co.kr//learn/courses/30/lessons/86052) | [빛의-경로-사이클.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/빛의-경로-사이클.js) | -| 29 | [삼각 달팽이](https://school.programmers.co.kr//learn/courses/30/lessons/68645) | [삼각-달팽이.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/삼각-달팽이.js) | -| 30 | [소수 찾기](https://school.programmers.co.kr//learn/courses/30/lessons/42839) | [소수-찾기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/소수-찾기.js) | -| 31 | [수식 최대화](https://school.programmers.co.kr//learn/courses/30/lessons/67257) | [수식-최대화.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/수식-최대화.js) | -| 32 | [숫자의 표현](https://school.programmers.co.kr//learn/courses/30/lessons/12924) | [숫자의-표현.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/숫자의-표현.js) | -| 33 | [스킬트리](https://school.programmers.co.kr//learn/courses/30/lessons/49993) | [스킬트리.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/스킬트리.js) | -| 34 | [영어 끝말잇기](https://school.programmers.co.kr//learn/courses/30/lessons/12981) | [영어-끝말잇기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/영어-끝말잇기.js) | -| 35 | [예상 대진표](https://school.programmers.co.kr//learn/courses/30/lessons/12985) | [예상-대진표.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/예상-대진표.js) | -| 36 | [오픈채팅방 ](https://school.programmers.co.kr//learn/courses/30/lessons/42888) | [오픈채팅방.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/오픈채팅방.js) | -| 37 | [올바른 괄호](https://school.programmers.co.kr//learn/courses/30/lessons/12909) | [올바른-괄호.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/올바른-괄호.js) | -| 38 | [위장](https://school.programmers.co.kr//learn/courses/30/lessons/42578) | [위장.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/위장.js) | -| 39 | [점프와 순간 이동](https://school.programmers.co.kr//learn/courses/30/lessons/12980) | [점프와-순간-이동.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/점프와-순간-이동.js) | -| 40 | [조이스틱](https://school.programmers.co.kr//learn/courses/30/lessons/42860) | [조이스틱.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/조이스틱.js) | -| 41 | [주차 요금 계산](https://school.programmers.co.kr//learn/courses/30/lessons/92341) | [주차-요금-계산.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/주차-요금-계산.js) | -| 42 | [짝지어 제거하기](https://school.programmers.co.kr//learn/courses/30/lessons/12973) | [짝지어-제거하기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/짝지어-제거하기.js) | -| 43 | [최댓값과 최솟값](https://school.programmers.co.kr//learn/courses/30/lessons/12939) | [최댓값과-최솟값.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/최댓값과-최솟값.js) | -| 44 | [최솟값 만들기](https://school.programmers.co.kr//learn/courses/30/lessons/12941) | [최솟값-만들기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/최솟값-만들기.js) | -| 45 | [카펫](https://school.programmers.co.kr//learn/courses/30/lessons/42842) | [카펫.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/카펫.js) | -| 46 | [큰 수 만들기](https://school.programmers.co.kr//learn/courses/30/lessons/42883) | [큰-수-만들기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/큰-수-만들기.js) | -| 47 | [타겟 넘버](https://school.programmers.co.kr//learn/courses/30/lessons/43165) | [타겟-넘버.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/타겟-넘버.js) | -| 48 | [튜플](https://school.programmers.co.kr//learn/courses/30/lessons/64065) | [튜플.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/튜플.js) | -| 49 | [프린터](https://school.programmers.co.kr//learn/courses/30/lessons/42587) | [프린터.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/프린터.js) | -| 50 | [피보나치 수](https://school.programmers.co.kr//learn/courses/30/lessons/12945) | [피보나치-수.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/피보나치-수.js) | -| 51 | [하노이의 탑](https://school.programmers.co.kr//learn/courses/30/lessons/12946) | [하노이의-탑.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/하노이의-탑.js) | -| 52 | [행렬 테두리 회전하기](https://school.programmers.co.kr//learn/courses/30/lessons/77485) | [행렬-테두리-회전하기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/행렬-테두리-회전하기.js) | -| 53 | [행렬의 곱셈](https://school.programmers.co.kr//learn/courses/30/lessons/12949) | [행렬의-곱셈.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/행렬의-곱셈.js) | -| 54 | [후보키](https://school.programmers.co.kr//learn/courses/30/lessons/42890) | [후보키.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/후보키.js) | + ### Level 3 👨🏻‍💻(풀이 중..) diff --git "a/level-2/\352\265\254\353\252\205\353\263\264\355\212\270.js" "b/level-2/\352\265\254\353\252\205\353\263\264\355\212\270.js" index c6819b5..3bfd2a7 100644 --- "a/level-2/\352\265\254\353\252\205\353\263\264\355\212\270.js" +++ "b/level-2/\352\265\254\353\252\205\353\263\264\355\212\270.js" @@ -40,4 +40,27 @@ function solution(people, limit) { } return count; +} + +// 정답 3 - iHoHyeon +function solution(people, limit) { + people.sort((a, b) => a - b); // 오름차순 정렬 + + let cnt = 0; // 구명보트 개수 + + let front = 0; // 가장 몸무게가 작은 사람의 index + let last = people.length; // 가장 몸무게가 큰 사람의 index + + while (last > front) { + cnt++; + + const now = people.pop(); + last--; + + if (now + people[front] <= limit) { + front++; // shift() 연산의 비효율로 인해 front 사용 + } + } + + return cnt; } \ No newline at end of file diff --git "a/level-2/\353\260\260\353\213\254.js" "b/level-2/\353\260\260\353\213\254.js" new file mode 100644 index 0000000..fb70325 --- /dev/null +++ "b/level-2/\353\260\260\353\213\254.js" @@ -0,0 +1,190 @@ +//https://github.com/codeisneverodd/programmers-coding-test +//완벽한 정답이 아닙니다. + +//정답 1 - pereng11 +// 다익스트라 + 최소힙 O( N * logN ) + // [목적지, 거리] 노드를 값으로 가지는, 거리에 대한 최소힙 +class MinHeap{ + constructor () + { + this.heap = [ null ]; + } + // 맨 끝에 노드를 삽입 후 위로 올라가면서 정렬 + push ( val ) + { + this.heap.push(val); + let childIdx = this.heap.length-1; + let parentIdx = Math.floor(childIdx / 2); + while(parentIdx > 0 && this.heap[parentIdx][1] > this.heap[childIdx][1]){ + this.swap( childIdx, parentIdx ); + childIdx = parentIdx; + parentIdx = Math.floor(childIdx / 2); + } + } + pop () + { + if ( this.heap.length === 1 ) + { + return undefined; + } + // 최소값은 빼두었다가 리턴하고, 가장 끝 값을 맨 위로 가져와 아래로 내려가면서 정렬 + const minNode = this.heap[ 1 ]; + this.heap[ 1 ] = this.heap[ this.heap.length - 1 ]; + this.heap.pop(); + let parentIdx = 1; + let leftChildIdx = 2; + let rightChildIdx = 3; + while ( parentIdx < this.heap.length ) + { + // 자식이 없는 경우 + if ( !this.heap[ leftChildIdx ] ) + { + break; + } // 왼쪽 자식만 있는 경우 + else if ( !this.heap[ rightChildIdx ] ) + { + if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] ) + { + this.swap( parentIdx, leftChildIdx ); + } + break; + // 둘 중 하나가 부모보다 작을 때, 더 작은 쪽으로 정렬 + } else if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] || this.heap[ parentIdx ][ 1 ] > this.heap[ rightChildIdx ][ 1 ] ) + { + const minChildIdx = this.heap[ leftChildIdx ][ 1 ] < this.heap[ rightChildIdx ][ 1 ] ? leftChildIdx : rightChildIdx; + this.swap( parentIdx, minChildIdx ); + parentIdx = minChildIdx; + leftChildIdx = parentIdx * 2 + rightChildIdx = parentIdx * 2 + 1; + } else + { + // 끝까지 내려가지 않았더라도 부모가 가장 작으면 정렬 중지 + break; + } + } + return minNode; + } + swap ( idx1, idx2 ) + { + [ this.heap[ idx1 ], this.heap[ idx2 ] ] = [ this.heap[ idx2 ], this.heap[ idx1 ] ]; + } + length () + { + return this.heap.length; + } +} + +function solution ( N, road, K ) +{ + const roadsTable = {}; //전체 도로 정보 + + // 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열 + for ( let i = 1; i <= N; i++ ) + { + roadsTable[ i ] = []; + } + road.forEach( road => + { + let [ sp, ep, dist ] = road; + roadsTable[ sp ].push( [ ep, dist ] ); + roadsTable[ ep ].push( [ sp, dist ] ); + } ); + + function djikstra ( sp ) + { + const visited = new Array( N + 1 ).fill( false ); //방문 확인 배열 + const dist = new Array( N + 1 ).fill( Infinity ); //목표지점까지 거리 + const heap = new MinHeap(); + + //시작점 삽입 + heap.push( [sp, 0] ); + + // 가장 가까운 목적지부터 순서대로 방문 + while ( heap.length() > 1 ) + { + //힙에 저장된 목적지 중 가장 가까운 거리의 목적지를 꺼냄 [목적지, 거리] + const [ ep, val ] = heap.pop(); + //아직 방문하지 않은 곳만 처리 + if ( !visited[ ep ] ) + { + //방문처리, 거리 저장 + visited[ ep ] = true; + dist[ ep ] = val; + //방문 지점을 거쳐서 가는 다른 목적지 구하기 + const nexts = roadsTable[ ep ]; + if ( nexts ) + { + nexts.forEach( n => + { + let [ nextEp, nextVal ] = n; + if ( !visited[ nextEp ] ) //아직 방문하지 않은 곳일 경우, '지금까지의 거리 + 현재 위치에서의 거리'로 힙에 삽입 + { + heap.push( [ nextEp, val + nextVal ] ); + } + }) + } + } + } + // 거리가 K이하인 지점의 개수 반환 + const result = dist.filter( d => d <= K ).length; + return result; + } + + const answer = djikstra( 1 ); + return answer; +} + +//정답 2 - pereng11 O(N * N); +//다익스트라 + 선형탐색 +function solution ( N, road, K ) +{ + const roadsTable = {}; //전체 도로 정보 + + // 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열 + for ( let i = 1; i <= N; i++ ) + { + roadsTable[ i ] = []; + } + road.forEach( road => + { + let [ sp, ep, dist ] = road; + roadsTable[ sp ].push( [ ep, dist ] ); + roadsTable[ ep ].push( [ sp, dist ] ); + } ); + + function djikstra ( sp ) + { + const dist = new Array( N + 1 ).fill( Infinity ); //목표지점까지 거리 + const queue = []; + + queue.push( [sp, 0] ); + + while ( queue.length > 0 ) + { + const [ ep, val ] = queue.shift(); + if ( dist[ ep ] > val ) + { + dist[ ep ] = val; + const nexts = roadsTable[ ep ]; + if ( nexts ) + { + nexts.forEach( n => + { + let [ nextEp, nextVal ] = n; + //거리가 더 줄어드는 경우, '지금까지의 거리 + 현재 위치에서의 거리'로 힙에 삽입 + if ( dist[ nextEp ] > val + nextVal ) + { + queue.push( [ nextEp, val + nextVal ] ); + } + } ); + } + } + } + // 거리가 K이하인 지점의 개수 반환 + const result = dist.filter( d => d <= K ).length; + return result; + } + + const answer = djikstra( 1 ); + return answer; +} \ No newline at end of file