Skip to content

Commit 72c60e7

Browse files
codeisneveroddchaerin-devgithub-actionspereng11iHoHyeon
authored
[18주차] 풀이 추가 (#78)
* solution: level 2 / 하노이 탑 풀이 추가 * solution: 모음사전 * solution 체육복 * solution 모의고사 * solution 위장 * solution 튜플 * 브랜치 흐름 정리 (#71) * docs: 커밋 컨벤션 수정 및 contributor 추가 * Automatic Update README.md * fix: 오타 수정 * docs: 커밋 컨벤션 변경 및 리드미 디자인 수정 * docs: 풀이 완료시점 수정 * Automatic Update README.md * docs: PR 템플릿 추가 Co-authored-by: github-actions <[email protected]> * fix: base url 핫픽스 (#74) * docs: 커밋 컨벤션 수정 및 contributor 추가 * Automatic Update README.md * fix: 오타 수정 * docs: 커밋 컨벤션 변경 및 리드미 디자인 수정 * docs: 풀이 완료시점 수정 * Automatic Update README.md * docs: PR 템플릿 추가 * fix: 링크 base url 수정 (#73) * Automatic Update README.md Co-authored-by: github-actions <[email protected]> * [손재영] level2 배달 풀이 및 주석 * solution 구명보트 * fix: 최소힙 클래스 바깥 분리&파일 컨벤션 적용 Co-authored-by: chaerin-dev <[email protected]> Co-authored-by: github-actions <[email protected]> Co-authored-by: Jaeyeong son <[email protected]> Co-authored-by: iHoHyeon <[email protected]>
1 parent 4b87ea3 commit 72c60e7

File tree

3 files changed

+214
-29
lines changed

3 files changed

+214
-29
lines changed

README.md

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,7 @@
136136
| 23 | [땅따먹기](https://school.programmers.co.kr//learn/courses/30/lessons/12913) | [땅따먹기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/땅따먹기.js) |
137137
| 24 | [멀쩡한 사각형](https://school.programmers.co.kr//learn/courses/30/lessons/62048) | [멀쩡한-사각형.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/멀쩡한-사각형.js) |
138138
| 25 | [메뉴 리뉴얼](https://school.programmers.co.kr//learn/courses/30/lessons/72411) | [메뉴-리뉴얼.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/메뉴-리뉴얼.js) |
139-
| 26 | [모음 사전](https://school.programmers.co.kr/undefined) | [모음-사전.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/모음-사전.js) |
140-
| 27 | [문자열 압축](https://school.programmers.co.kr//learn/courses/30/lessons/60057) | [문자열-압축.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/문자열-압축.js) |
141-
| 28 | [빛의 경로 사이클](https://school.programmers.co.kr//learn/courses/30/lessons/86052) | [빛의-경로-사이클.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/빛의-경로-사이클.js) |
142-
| 29 | [삼각 달팽이](https://school.programmers.co.kr//learn/courses/30/lessons/68645) | [삼각-달팽이.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/삼각-달팽이.js) |
143-
| 30 | [소수 찾기](https://school.programmers.co.kr//learn/courses/30/lessons/42839) | [소수-찾기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/소수-찾기.js) |
144-
| 31 | [수식 최대화](https://school.programmers.co.kr//learn/courses/30/lessons/67257) | [수식-최대화.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/수식-최대화.js) |
145-
| 32 | [숫자의 표현](https://school.programmers.co.kr//learn/courses/30/lessons/12924) | [숫자의-표현.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/숫자의-표현.js) |
146-
| 33 | [스킬트리](https://school.programmers.co.kr//learn/courses/30/lessons/49993) | [스킬트리.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/스킬트리.js) |
147-
| 34 | [영어 끝말잇기](https://school.programmers.co.kr//learn/courses/30/lessons/12981) | [영어-끝말잇기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/영어-끝말잇기.js) |
148-
| 35 | [예상 대진표](https://school.programmers.co.kr//learn/courses/30/lessons/12985) | [예상-대진표.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/예상-대진표.js) |
149-
| 36 | [오픈채팅방 ](https://school.programmers.co.kr//learn/courses/30/lessons/42888) | [오픈채팅방.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/오픈채팅방.js) |
150-
| 37 | [올바른 괄호](https://school.programmers.co.kr//learn/courses/30/lessons/12909) | [올바른-괄호.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/올바른-괄호.js) |
151-
| 38 | [위장](https://school.programmers.co.kr//learn/courses/30/lessons/42578) | [위장.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/위장.js) |
152-
| 39 | [점프와 순간 이동](https://school.programmers.co.kr//learn/courses/30/lessons/12980) | [점프와-순간-이동.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/점프와-순간-이동.js) |
153-
| 40 | [조이스틱](https://school.programmers.co.kr//learn/courses/30/lessons/42860) | [조이스틱.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/조이스틱.js) |
154-
| 41 | [주차 요금 계산](https://school.programmers.co.kr//learn/courses/30/lessons/92341) | [주차-요금-계산.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/주차-요금-계산.js) |
155-
| 42 | [짝지어 제거하기](https://school.programmers.co.kr//learn/courses/30/lessons/12973) | [짝지어-제거하기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/짝지어-제거하기.js) |
156-
| 43 | [최댓값과 최솟값](https://school.programmers.co.kr//learn/courses/30/lessons/12939) | [최댓값과-최솟값.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/최댓값과-최솟값.js) |
157-
| 44 | [최솟값 만들기](https://school.programmers.co.kr//learn/courses/30/lessons/12941) | [최솟값-만들기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/최솟값-만들기.js) |
158-
| 45 | [카펫](https://school.programmers.co.kr//learn/courses/30/lessons/42842) | [카펫.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/카펫.js) |
159-
| 46 | [큰 수 만들기](https://school.programmers.co.kr//learn/courses/30/lessons/42883) | [큰-수-만들기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/큰-수-만들기.js) |
160-
| 47 | [타겟 넘버](https://school.programmers.co.kr//learn/courses/30/lessons/43165) | [타겟-넘버.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/타겟-넘버.js) |
161-
| 48 | [튜플](https://school.programmers.co.kr//learn/courses/30/lessons/64065) | [튜플.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/튜플.js) |
162-
| 49 | [프린터](https://school.programmers.co.kr//learn/courses/30/lessons/42587) | [프린터.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/프린터.js) |
163-
| 50 | [피보나치 수](https://school.programmers.co.kr//learn/courses/30/lessons/12945) | [피보나치-수.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/피보나치-수.js) |
164-
| 51 | [하노이의 탑](https://school.programmers.co.kr//learn/courses/30/lessons/12946) | [하노이의-탑.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/하노이의-탑.js) |
165-
| 52 | [행렬 테두리 회전하기](https://school.programmers.co.kr//learn/courses/30/lessons/77485) | [행렬-테두리-회전하기.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/행렬-테두리-회전하기.js) |
166-
| 53 | [행렬의 곱셈](https://school.programmers.co.kr//learn/courses/30/lessons/12949) | [행렬의-곱셈.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/행렬의-곱셈.js) |
167-
| 54 | [후보키](https://school.programmers.co.kr//learn/courses/30/lessons/42890) | [후보키.js](https://github.com/codeisneverodd/programmers-coding-test/blob/main/level-2/후보키.js) |
139+
168140

169141
### Level 3 👨🏻‍💻(풀이 중..)
170142

level-2/구명보트.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,27 @@ function solution(people, limit) {
4040
}
4141

4242
return count;
43+
}
44+
45+
// 정답 3 - iHoHyeon
46+
function solution(people, limit) {
47+
people.sort((a, b) => a - b); // 오름차순 정렬
48+
49+
let cnt = 0; // 구명보트 개수
50+
51+
let front = 0; // 가장 몸무게가 작은 사람의 index
52+
let last = people.length; // 가장 몸무게가 큰 사람의 index
53+
54+
while (last > front) {
55+
cnt++;
56+
57+
const now = people.pop();
58+
last--;
59+
60+
if (now + people[front] <= limit) {
61+
front++; // shift() 연산의 비효율로 인해 front 사용
62+
}
63+
}
64+
65+
return cnt;
4366
}

level-2/배달.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
4+
//정답 1 - pereng11
5+
// 다익스트라 + 최소힙 O( N * logN )
6+
// [목적지, 거리] 노드를 값으로 가지는, 거리에 대한 최소힙
7+
class MinHeap{
8+
constructor ()
9+
{
10+
this.heap = [ null ];
11+
}
12+
// 맨 끝에 노드를 삽입 후 위로 올라가면서 정렬
13+
push ( val )
14+
{
15+
this.heap.push(val);
16+
let childIdx = this.heap.length-1;
17+
let parentIdx = Math.floor(childIdx / 2);
18+
while(parentIdx > 0 && this.heap[parentIdx][1] > this.heap[childIdx][1]){
19+
this.swap( childIdx, parentIdx );
20+
childIdx = parentIdx;
21+
parentIdx = Math.floor(childIdx / 2);
22+
}
23+
}
24+
pop ()
25+
{
26+
if ( this.heap.length === 1 )
27+
{
28+
return undefined;
29+
}
30+
// 최소값은 빼두었다가 리턴하고, 가장 끝 값을 맨 위로 가져와 아래로 내려가면서 정렬
31+
const minNode = this.heap[ 1 ];
32+
this.heap[ 1 ] = this.heap[ this.heap.length - 1 ];
33+
this.heap.pop();
34+
let parentIdx = 1;
35+
let leftChildIdx = 2;
36+
let rightChildIdx = 3;
37+
while ( parentIdx < this.heap.length )
38+
{
39+
// 자식이 없는 경우
40+
if ( !this.heap[ leftChildIdx ] )
41+
{
42+
break;
43+
} // 왼쪽 자식만 있는 경우
44+
else if ( !this.heap[ rightChildIdx ] )
45+
{
46+
if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] )
47+
{
48+
this.swap( parentIdx, leftChildIdx );
49+
}
50+
break;
51+
// 둘 중 하나가 부모보다 작을 때, 더 작은 쪽으로 정렬
52+
} else if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] || this.heap[ parentIdx ][ 1 ] > this.heap[ rightChildIdx ][ 1 ] )
53+
{
54+
const minChildIdx = this.heap[ leftChildIdx ][ 1 ] < this.heap[ rightChildIdx ][ 1 ] ? leftChildIdx : rightChildIdx;
55+
this.swap( parentIdx, minChildIdx );
56+
parentIdx = minChildIdx;
57+
leftChildIdx = parentIdx * 2
58+
rightChildIdx = parentIdx * 2 + 1;
59+
} else
60+
{
61+
// 끝까지 내려가지 않았더라도 부모가 가장 작으면 정렬 중지
62+
break;
63+
}
64+
}
65+
return minNode;
66+
}
67+
swap ( idx1, idx2 )
68+
{
69+
[ this.heap[ idx1 ], this.heap[ idx2 ] ] = [ this.heap[ idx2 ], this.heap[ idx1 ] ];
70+
}
71+
length ()
72+
{
73+
return this.heap.length;
74+
}
75+
}
76+
77+
function solution ( N, road, K )
78+
{
79+
const roadsTable = {}; //전체 도로 정보
80+
81+
// 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열
82+
for ( let i = 1; i <= N; i++ )
83+
{
84+
roadsTable[ i ] = [];
85+
}
86+
road.forEach( road =>
87+
{
88+
let [ sp, ep, dist ] = road;
89+
roadsTable[ sp ].push( [ ep, dist ] );
90+
roadsTable[ ep ].push( [ sp, dist ] );
91+
} );
92+
93+
function djikstra ( sp )
94+
{
95+
const visited = new Array( N + 1 ).fill( false ); //방문 확인 배열
96+
const dist = new Array( N + 1 ).fill( Infinity ); //목표지점까지 거리
97+
const heap = new MinHeap();
98+
99+
//시작점 삽입
100+
heap.push( [sp, 0] );
101+
102+
// 가장 가까운 목적지부터 순서대로 방문
103+
while ( heap.length() > 1 )
104+
{
105+
//힙에 저장된 목적지 중 가장 가까운 거리의 목적지를 꺼냄 [목적지, 거리]
106+
const [ ep, val ] = heap.pop();
107+
//아직 방문하지 않은 곳만 처리
108+
if ( !visited[ ep ] )
109+
{
110+
//방문처리, 거리 저장
111+
visited[ ep ] = true;
112+
dist[ ep ] = val;
113+
//방문 지점을 거쳐서 가는 다른 목적지 구하기
114+
const nexts = roadsTable[ ep ];
115+
if ( nexts )
116+
{
117+
nexts.forEach( n =>
118+
{
119+
let [ nextEp, nextVal ] = n;
120+
if ( !visited[ nextEp ] ) //아직 방문하지 않은 곳일 경우, '지금까지의 거리 + 현재 위치에서의 거리'로 힙에 삽입
121+
{
122+
heap.push( [ nextEp, val + nextVal ] );
123+
}
124+
})
125+
}
126+
}
127+
}
128+
// 거리가 K이하인 지점의 개수 반환
129+
const result = dist.filter( d => d <= K ).length;
130+
return result;
131+
}
132+
133+
const answer = djikstra( 1 );
134+
return answer;
135+
}
136+
137+
//정답 2 - pereng11 O(N * N);
138+
//다익스트라 + 선형탐색
139+
function solution ( N, road, K )
140+
{
141+
const roadsTable = {}; //전체 도로 정보
142+
143+
// 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열
144+
for ( let i = 1; i <= N; i++ )
145+
{
146+
roadsTable[ i ] = [];
147+
}
148+
road.forEach( road =>
149+
{
150+
let [ sp, ep, dist ] = road;
151+
roadsTable[ sp ].push( [ ep, dist ] );
152+
roadsTable[ ep ].push( [ sp, dist ] );
153+
} );
154+
155+
function djikstra ( sp )
156+
{
157+
const dist = new Array( N + 1 ).fill( Infinity ); //목표지점까지 거리
158+
const queue = [];
159+
160+
queue.push( [sp, 0] );
161+
162+
while ( queue.length > 0 )
163+
{
164+
const [ ep, val ] = queue.shift();
165+
if ( dist[ ep ] > val )
166+
{
167+
dist[ ep ] = val;
168+
const nexts = roadsTable[ ep ];
169+
if ( nexts )
170+
{
171+
nexts.forEach( n =>
172+
{
173+
let [ nextEp, nextVal ] = n;
174+
//거리가 더 줄어드는 경우, '지금까지의 거리 + 현재 위치에서의 거리'로 힙에 삽입
175+
if ( dist[ nextEp ] > val + nextVal )
176+
{
177+
queue.push( [ nextEp, val + nextVal ] );
178+
}
179+
} );
180+
}
181+
}
182+
}
183+
// 거리가 K이하인 지점의 개수 반환
184+
const result = dist.filter( d => d <= K ).length;
185+
return result;
186+
}
187+
188+
const answer = djikstra( 1 );
189+
return answer;
190+
}

0 commit comments

Comments
 (0)