-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed7eee2
commit 285d633
Showing
7 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,64 @@ | ||
class MaxHeap { | ||
constructor() { | ||
this.storage = [null]; | ||
} | ||
|
||
insert(value) { | ||
this.storage.push(value); | ||
let index = this.storage.length - 1; | ||
while (index > 1) { | ||
let parentIndex = Math.floor(index / 2); | ||
if (this.storage[parentIndex] < this.storage[index]) { | ||
let temp = this.storage[index]; | ||
this.storage[index] = this.storage[parentIndex]; | ||
this.storage[parentIndex] = temp; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
delete(value) { | ||
let lastIndex = this.storage.length - 1; | ||
let temp = this.storage[lastIndex]; | ||
this.storage[lastIndex] = this.storage[1]; | ||
this.storage[1] = temp; | ||
let targetIndex = 1; | ||
let prevMax = this.storage.pop(); | ||
|
||
while (targetIndex <= this.storage.length - 1) { | ||
let leftChildIndex = targetIndex * 2; | ||
let rightChildIndex = targetIndex * 2 + 1; | ||
let maxIndex = targetIndex; | ||
|
||
if ( | ||
leftChildIndex <= this.storage.length - 1 && | ||
this.storage[leftChildIndex] > this.storage[maxIndex] | ||
) | ||
maxIndex = leftChildIndex; | ||
if ( | ||
rightChildIndex <= this.storage.length - 1 && | ||
this.storage[rightChildIndex] > this.storage[maxIndex] | ||
) | ||
maxIndex = rightChildIndex; | ||
if (maxIndex === targetIndex) break; | ||
|
||
let targetValue = this.storage[targetIndex]; | ||
this.storage[targetIndex] = this.storage[maxIndex]; | ||
this.storage[maxIndex] = targetValue; | ||
targetIndex = maxIndex; | ||
} | ||
return prevMax; | ||
} | ||
} | ||
|
||
const maxHeap = new MaxHeap(); | ||
maxHeap.insert(8); | ||
maxHeap.insert(6); | ||
maxHeap.insert(7); | ||
maxHeap.insert(2); | ||
maxHeap.insert(5); | ||
maxHeap.insert(4); | ||
console.log(maxHeap.storage); // [None, 8, 6, 7, 2, 5, 4] | ||
console.log(maxHeap.delete()); // 8 을 반환해야 합니다! | ||
console.log(maxHeap.storage); // [None, 7, 6, 4, 2, 5] |
This file contains 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,30 @@ | ||
const graph = { | ||
1: [2, 5, 9], | ||
2: [1, 3], | ||
3: [2, 4], | ||
4: [3], | ||
5: [1, 6, 8], | ||
6: [5, 7], | ||
7: [6], | ||
8: [5], | ||
9: [1, 10], | ||
10: [9], | ||
}; | ||
|
||
const visited = []; | ||
|
||
function dfsRecursion(adjacentGraph, currentNode, visitedAry) { | ||
visitedAry.push(currentNode); | ||
|
||
adjacentGraph[currentNode].forEach((node) => { | ||
if (visited.indexOf(node) === -1) { | ||
return dfsRecursion(adjacentGraph, node, visitedAry); | ||
} | ||
}); | ||
} | ||
|
||
if (adjacentGraph[currentNode].length === 1) return; | ||
// 원래는 탈출 조건을 따로 줬는데, 쓸모가 없었다. => 어짜피 forEach로 돌려서 만약 if문이 해당 안되면 부모 노드에서 그 다음 요소가 대기타고 있으므로 괜찮음 | ||
|
||
dfsRecursion(graph, 1, visited); | ||
console.log(visited); |
This file contains 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,34 @@ | ||
// DFS - stack으로 구현하기 | ||
|
||
const graph = { | ||
1: [2, 5, 9], | ||
2: [1, 3], | ||
3: [2, 4], | ||
4: [3], | ||
5: [1, 6, 8], | ||
6: [5, 7], | ||
7: [6], | ||
8: [5], | ||
9: [1, 10], | ||
10: [9], | ||
}; | ||
|
||
function dfsStack(adjacent_graph, startNode) { | ||
const visited = []; | ||
const stack = [startNode]; | ||
|
||
while (stack.length !== 0) { | ||
let popNode = stack.pop(); | ||
visited.push(popNode); | ||
|
||
adjacent_graph[popNode].forEach((node) => { | ||
if (visited.indexOf(node) === -1) { | ||
stack.push(node); | ||
} | ||
}); | ||
} | ||
return visited; | ||
} | ||
|
||
console.log(dfsStack(graph, 1)); // 1 이 시작노드입니다! | ||
// [1, 9, 10, 5, 8, 6, 7, 2, 3, 4] 이 출력되어야 합니다! |
This file contains 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,30 @@ | ||
// BFS - queue로 구현 | ||
|
||
const graph = { | ||
1: [2, 3, 4], | ||
2: [1, 5], | ||
3: [1, 6, 7], | ||
4: [1, 8], | ||
5: [2, 9], | ||
6: [3, 10], | ||
7: [3], | ||
8: [4], | ||
9: [5], | ||
10: [6], | ||
}; | ||
|
||
function bfsQueue(adjGraph, startNode) { | ||
const queue = [startNode]; | ||
const visited = []; | ||
|
||
while (queue.length > 0) { | ||
let shiftNode = queue.shift(); | ||
visited.push(shiftNode); | ||
adjGraph[shiftNode].forEach((node) => { | ||
if (visited.indexOf(node) === -1) queue.push(node); | ||
}); | ||
} | ||
return visited; | ||
} | ||
|
||
console.log(bfsQueue(graph, 1)); |
This file contains 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,13 @@ | ||
// 피보나치 수열 - 재귀함수 ver | ||
|
||
let input = 100; | ||
|
||
function fiboRecursive(n) { | ||
if (n === 1 || n === 2) return 1; | ||
return fiboRecursive(n - 1) + fiboRecursive(n - 2); | ||
} | ||
|
||
// fiboRecursive(19) + fiboRecursive(18) | ||
// (fiboRecursive(18) + fiboRecursive(17)) + (fiboRecursive(17) + fiboRecursive(16)) ………………… | ||
|
||
console.log(fiboRecursive(input)); |
This file contains 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,21 @@ | ||
// 피보나치 수열 - 동적 계획법 사용 | ||
|
||
const input = 100; | ||
const fiboMemo = [0, 1, 1]; | ||
|
||
function fiboDynamic(n, fiboMemo) { | ||
for (let i = 3; i <= n; i++) { | ||
fiboMemo[i] = fiboMemo[i - 1] + fiboMemo[i - 2]; | ||
} | ||
return fiboMemo[n]; | ||
} | ||
|
||
// function fiboDynamic(n, fiboMemo) { | ||
// if (n === 1 || n === 2) return fiboMemo[n]; | ||
|
||
// let nthFibo = fiboDynamic(n - 1) | fiboDynamic(n - 2); | ||
// fiboMemo[n] = nthFibo; | ||
// return nthFibo; | ||
// } | ||
|
||
console.log(fiboDynamic(input, fiboMemo)); |
This file contains 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,39 @@ | ||
// 농심 라면 공장 | ||
|
||
// 라면 공장에서는 하루에 밀가루를 1톤씩 사용합니다. 원래 밀가루를 공급받던 공장의 고장으로 앞으로 k일 이후에야 밀가루를 공급받을 수 있기 때문에 해외 공장에서 밀가루를 수입해야 합니다. | ||
|
||
// 해외 공장에서는 향후 밀가루를 공급할 수 있는 날짜와 수량을 알려주었고, 라면 공장에서는 운송비를 줄이기 위해 최소한의 횟수로 밀가루를 공급받고 싶습니다. | ||
|
||
// 현재 공장에 남아있는 밀가루 수량 stock, 밀가루 공급 일정(dates)과 해당 시점에 공급 가능한 밀가루 수량(supplies), 원래 공장으로부터 공급받을 수 있는 시점 k가 주어질 때, 밀가루가 떨어지지 않고 공장을 운영하기 위해서 최소한 몇 번 해외 공장으로부터 밀가루를 공급받아야 하는지를 반환 하시오. | ||
|
||
// dates[i]에는 i번째 공급 가능일이 들어있으며, supplies[i]에는 dates[i] 날짜에 공급 가능한 밀가루 수량이 들어 있습니다 | ||
|
||
import { Heap } from "heap-js"; | ||
|
||
let stock = 4; | ||
let dates = [4, 10, 15]; | ||
let supplies = [20, 5, 10]; | ||
let k = 30; | ||
|
||
function getCount(stock, dates, supplies, k) { | ||
let lastAddedDateIndex = 0; // dates 배열 중 이번에 추가되기 시작할 index값 | ||
let heap = []; // heap으로 사용할 배열(minHeap임 최솟값이 rootNode) | ||
let count = 0; // 구해야 할 값 | ||
|
||
while (stock <= k) { | ||
// stock이 k보다 크면 더 이상 공급 받을 이유가 없음 | ||
while ( | ||
lastAddedDateIndex < dates.length && // dates.length 이상으로 넣을 수 없음 ㅎㅎ | ||
dates[lastAddedDateIndex] <= stock // 해당 날짜보다 stock이 많이 남아있어야 공백없이 버틸 수 있음 | ||
) { | ||
Heap.heappush(heap, -supplies[lastAddedDateIndex]); // 넣을 수 있는 만큼 heap에 넣음 (minHeap이므로 최댓값이 루트가 되도록 -를 곱해줌) | ||
lastAddedDateIndex += 1; | ||
} | ||
count++; | ||
let heappop = Heap.heappop(heap); // heappop을 하면 루트노드가 반환됨 | ||
stock += -heappop; | ||
} | ||
return count; | ||
} | ||
|
||
console.log(getCount(stock, dates, supplies, k)); |