-
Notifications
You must be signed in to change notification settings - Fork 98
[18주차] 풀이 추가 #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[18주차] 풀이 추가 #78
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
a18681d
solution: level 2 / 하노이 탑 풀이 추가
codeisneverodd 32adc56
solution: 모음사전
codeisneverodd 876d450
solution 체육복
chaerin-dev 8127da5
solution 모의고사
chaerin-dev 8061345
solution 위장
chaerin-dev 4fb5408
solution 튜플
chaerin-dev 1cbfe92
브랜치 흐름 정리 (#71)
codeisneverodd 2df8161
Merge branch 'main' into week
codeisneverodd 4440efc
fix: base url 핫픽스 (#74)
codeisneverodd 6f946ef
Merge branch 'main' into week
codeisneverodd 031c52d
[손재영] level2 배달 풀이 및 주석
pereng11 d2b9fd3
solution 구명보트
iHoHyeon 0cd0d67
Merge branch 'week' into main
codeisneverodd 1ae2b59
Merge pull request #77 from iHoHyeon/main
codeisneverodd 8938fca
fix: 최소힙 클래스 바깥 분리&파일 컨벤션 적용
pereng11 f16e1fe
Merge branch 'week' into jaeyeong
codeisneverodd 9a020a1
Merge pull request #76 from pereng11/jaeyeong
codeisneverodd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
level-2/배달.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.