Skip to content

SeoGeonhyuk / 3주차, 4주차 / 2문제 #32

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
SeoGeonhyuk/.DS_Store
27 changes: 27 additions & 0 deletions SeoGeonhyuk/dynamic_programming/integertriangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
let answer = 0;
function maxPathCalculator(floorArray, arrays){
if(arrays.length === 0){
floorArray.forEach((num) => {
if (answer <= num)
answer = num;
})
}
else{
const partArray = arrays.shift();
const futureFloorArray = new Array(partArray.length).fill(0);
for(let i = 0; i < floorArray.length; i++){
const possiblePathSum = floorArray[i] + partArray[i];
if(possiblePathSum > futureFloorArray[i])
futureFloorArray[i] = possiblePathSum;
if(i + 1 < partArray.length)
futureFloorArray[i + 1] = (floorArray[i] + partArray[i + 1]);
}
maxPathCalculator(futureFloorArray, arrays);
}
}
function solution(triangle) {
const oldSum = triangle[0][0];
triangle.shift();
maxPathCalculator([oldSum], triangle);
return answer;
}
88 changes: 88 additions & 0 deletions SeoGeonhyuk/heap/morehot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class MinHeap {
constructor() {
this.heap = [];
}

getParentIndex(index) {
return Math.floor((index - 1) / 2);
}

getLeftChildIndex(index) {
return index * 2 + 1;
}

getRightChildIndex(index) {
return index * 2 + 2;
}

insert(value) {
this.heap.push(value);
this.heapifyUp();
}

heapifyUp() {
let index = this.heap.length - 1;
while (index > 0) {
let parentIndex = this.getParentIndex(index);
if (this.heap[parentIndex] > this.heap[index]) {
[this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
index = parentIndex;
} else {
break;
}
}
}

extractMin() {
if (this.heap.length === 1) return this.heap.pop();
const min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown();
return min;
}

heapifyDown() {
let index = 0;
while (this.getLeftChildIndex(index) < this.heap.length) {
let smallerChildIndex = this.getLeftChildIndex(index);
if (this.getRightChildIndex(index) < this.heap.length && this.heap[this.getRightChildIndex(index)] < this.heap[smallerChildIndex]) {
smallerChildIndex = this.getRightChildIndex(index);
}
if (this.heap[index] > this.heap[smallerChildIndex]) {
[this.heap[index], this.heap[smallerChildIndex]] = [this.heap[smallerChildIndex], this.heap[index]];
index = smallerChildIndex;
} else {
break;
}
}
}

size() {
return this.heap.length;
}

peek() {
return this.heap[0];
}
}

function solution(scoville, K) {
const minHeap = new MinHeap();
scoville.forEach(num => minHeap.insert(num));

let answer = 0;

while (minHeap.size() > 1 && minHeap.peek() < K) {
let first = minHeap.extractMin();
let second = minHeap.extractMin();
let newScoville = first + (second * 2);
minHeap.insert(newScoville);
answer++;
}

if (minHeap.peek() < K) {
return -1;
}

return answer;
}