Skip to content

Commit 8938fca

Browse files
committed
fix: 최소힙 클래스 바깥 분리&파일 컨벤션 적용
1 parent 031c52d commit 8938fca

File tree

1 file changed

+65
-64
lines changed

1 file changed

+65
-64
lines changed

level-2/배달.js

Lines changed: 65 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,81 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//완벽한 정답이 아닙니다.
3+
14
//정답 1 - pereng11
25
// 다익스트라 + 최소힙 O( N * logN )
3-
function solution ( N, road, K )
4-
{
56
// [목적지, 거리] 노드를 값으로 가지는, 거리에 대한 최소힙
6-
class MinHeap{
7-
constructor ()
8-
{
9-
this.heap = [ null ];
10-
}
11-
// 맨 끝에 노드를 삽입 후 위로 올라가면서 정렬
12-
push ( val )
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 )
1327
{
14-
this.heap.push(val);
15-
let childIdx = this.heap.length-1;
16-
let parentIdx = Math.floor(childIdx / 2);
17-
while(parentIdx > 0 && this.heap[parentIdx][1] > this.heap[childIdx][1]){
18-
this.swap( childIdx, parentIdx );
19-
childIdx = parentIdx;
20-
parentIdx = Math.floor(childIdx / 2);
21-
}
28+
return undefined;
2229
}
23-
pop ()
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 )
2438
{
25-
if ( this.heap.length === 1 )
39+
// 자식이 없는 경우
40+
if ( !this.heap[ leftChildIdx ] )
2641
{
27-
return undefined;
28-
}
29-
// 최소값은 빼두었다가 리턴하고, 가장 끝 값을 맨 위로 가져와 아래로 내려가면서 정렬
30-
const minNode = this.heap[ 1 ];
31-
this.heap[ 1 ] = this.heap[ this.heap.length - 1 ];
32-
this.heap.pop();
33-
let parentIdx = 1;
34-
let leftChildIdx = 2;
35-
let rightChildIdx = 3;
36-
while ( parentIdx < this.heap.length )
42+
break;
43+
} // 왼쪽 자식만 있는 경우
44+
else if ( !this.heap[ rightChildIdx ] )
3745
{
38-
// 자식이 없는 경우
39-
if ( !this.heap[ leftChildIdx ] )
40-
{
41-
break;
42-
} // 왼쪽 자식만 있는 경우
43-
else if ( !this.heap[ rightChildIdx ] )
44-
{
45-
if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] )
46-
{
47-
this.swap( parentIdx, leftChildIdx );
48-
}
49-
break;
50-
// 둘 중 하나가 부모보다 작을 때, 더 작은 쪽으로 정렬
51-
} else if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] || this.heap[ parentIdx ][ 1 ] > this.heap[ rightChildIdx ][ 1 ] )
52-
{
53-
const minChildIdx = this.heap[ leftChildIdx ][ 1 ] < this.heap[ rightChildIdx ][ 1 ] ? leftChildIdx : rightChildIdx;
54-
this.swap( parentIdx, minChildIdx );
55-
parentIdx = minChildIdx;
56-
leftChildIdx = parentIdx * 2
57-
rightChildIdx = parentIdx * 2 + 1;
58-
} else
46+
if ( this.heap[ parentIdx ][ 1 ] > this.heap[ leftChildIdx ][ 1 ] )
5947
{
60-
// 끝까지 내려가지 않았더라도 부모가 가장 작으면 정렬 중지
61-
break;
48+
this.swap( parentIdx, leftChildIdx );
6249
}
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;
6363
}
64-
return minNode;
6564
}
66-
swap ( idx1, idx2 )
67-
{
68-
[ this.heap[ idx1 ], this.heap[ idx2 ] ] = [ this.heap[ idx2 ], this.heap[ idx1 ] ];
69-
}
70-
length ()
71-
{
72-
return this.heap.length;
73-
}
74-
}
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+
}
7576

76-
77+
function solution ( N, road, K )
78+
{
7779
const roadsTable = {}; //전체 도로 정보
7880

7981
// 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열
@@ -136,7 +138,6 @@ function solution ( N, road, K )
136138
//다익스트라 + 선형탐색
137139
function solution ( N, road, K )
138140
{
139-
140141
const roadsTable = {}; //전체 도로 정보
141142

142143
// 도로 정보 초기화 roadTable[시작점] = [목적지, 거리] 배열

0 commit comments

Comments
 (0)